Skip to content

Commit 9b3a8a3

Browse files
committed
Merge from mainline.
llvm-svn: 51869
1 parent a4f4614 commit 9b3a8a3

File tree

2 files changed

+60
-20
lines changed

2 files changed

+60
-20
lines changed

llvm/lib/Target/CBackend/CBackend.cpp

Lines changed: 46 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ namespace {
155155

156156
void writeOperand(Value *Operand);
157157
void writeOperandRaw(Value *Operand);
158+
void writeInstComputationInline(Instruction &I);
158159
void writeOperandInternal(Value *Operand);
159160
void writeOperandWithCast(Value* Operand, unsigned Opcode);
160161
void writeOperandWithCast(Value* Operand, const ICmpInst &I);
@@ -1217,12 +1218,32 @@ std::string CWriter::GetValueName(const Value *Operand) {
12171218
return Name;
12181219
}
12191220

1221+
/// writeInstComputationInline - Emit the computation for the specified
1222+
/// instruction inline, with no destination provided.
1223+
void CWriter::writeInstComputationInline(Instruction &I) {
1224+
// If this is a non-trivial bool computation, make sure to truncate down to
1225+
// a 1 bit value. This is important because we want "add i1 x, y" to return
1226+
// "0" when x and y are true, not "2" for example.
1227+
bool NeedBoolTrunc = false;
1228+
if (I.getType() == Type::Int1Ty && !isa<ICmpInst>(I) && !isa<FCmpInst>(I))
1229+
NeedBoolTrunc = true;
1230+
1231+
if (NeedBoolTrunc)
1232+
Out << "((";
1233+
1234+
visit(I);
1235+
1236+
if (NeedBoolTrunc)
1237+
Out << ")&1)";
1238+
}
1239+
1240+
12201241
void CWriter::writeOperandInternal(Value *Operand) {
12211242
if (Instruction *I = dyn_cast<Instruction>(Operand))
1243+
// Should we inline this instruction to build a tree?
12221244
if (isInlinableInst(*I) && !isDirectAlloca(I)) {
1223-
// Should we inline this instruction to build a tree?
12241245
Out << '(';
1225-
visit(*I);
1246+
writeInstComputationInline(*I);
12261247
Out << ')';
12271248
return;
12281249
}
@@ -2146,12 +2167,12 @@ void CWriter::printBasicBlock(BasicBlock *BB) {
21462167
outputLValue(II);
21472168
else
21482169
Out << " ";
2149-
visit(*II);
2170+
writeInstComputationInline(*II);
21502171
Out << ";\n";
21512172
}
21522173
}
21532174

2154-
// Don't emit prefix or suffix for the terminator...
2175+
// Don't emit prefix or suffix for the terminator.
21552176
visit(*BB->getTerminator());
21562177
}
21572178

@@ -2475,29 +2496,34 @@ static const char * getFloatBitCastField(const Type *Ty) {
24752496
void CWriter::visitCastInst(CastInst &I) {
24762497
const Type *DstTy = I.getType();
24772498
const Type *SrcTy = I.getOperand(0)->getType();
2478-
Out << '(';
24792499
if (isFPIntBitCast(I)) {
2500+
Out << '(';
24802501
// These int<->float and long<->double casts need to be handled specially
24812502
Out << GetValueName(&I) << "__BITCAST_TEMPORARY."
24822503
<< getFloatBitCastField(I.getOperand(0)->getType()) << " = ";
24832504
writeOperand(I.getOperand(0));
24842505
Out << ", " << GetValueName(&I) << "__BITCAST_TEMPORARY."
24852506
<< getFloatBitCastField(I.getType());
2486-
} else {
2487-
printCast(I.getOpcode(), SrcTy, DstTy);
2488-
if (I.getOpcode() == Instruction::SExt && SrcTy == Type::Int1Ty) {
2489-
// Make sure we really get a sext from bool by subtracing the bool from 0
2490-
Out << "0-";
2491-
}
2492-
writeOperand(I.getOperand(0));
2493-
if (DstTy == Type::Int1Ty &&
2494-
(I.getOpcode() == Instruction::Trunc ||
2495-
I.getOpcode() == Instruction::FPToUI ||
2496-
I.getOpcode() == Instruction::FPToSI ||
2497-
I.getOpcode() == Instruction::PtrToInt)) {
2498-
// Make sure we really get a trunc to bool by anding the operand with 1
2499-
Out << "&1u";
2500-
}
2507+
Out << ')';
2508+
return;
2509+
}
2510+
2511+
Out << '(';
2512+
printCast(I.getOpcode(), SrcTy, DstTy);
2513+
2514+
// Make a sext from i1 work by subtracting the i1 from 0 (an int).
2515+
if (SrcTy == Type::Int1Ty && I.getOpcode() == Instruction::SExt)
2516+
Out << "0-";
2517+
2518+
writeOperand(I.getOperand(0));
2519+
2520+
if (DstTy == Type::Int1Ty &&
2521+
(I.getOpcode() == Instruction::Trunc ||
2522+
I.getOpcode() == Instruction::FPToUI ||
2523+
I.getOpcode() == Instruction::FPToSI ||
2524+
I.getOpcode() == Instruction::PtrToInt)) {
2525+
// Make sure we really get a trunc to bool by anding the operand with 1
2526+
Out << "&1u";
25012527
}
25022528
Out << ')';
25032529
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
; RUN: llvm-as < %s | llc -march=c | grep {llvm_cbe_t.*&1}
2+
define i32 @test(i32 %r) {
3+
%s = icmp eq i32 %r, 0
4+
%t = add i1 %s, %s
5+
%u = zext i1 %t to i32
6+
br i1 %t, label %A, label %B
7+
A:
8+
9+
ret i32 %u
10+
B:
11+
12+
%v = select i1 %t, i32 %r, i32 %u
13+
ret i32 %v
14+
}

0 commit comments

Comments
 (0)