llvm.org GIT mirror llvm / 4c89fb9
Merge from mainline: Fix PR1666, SPASS with the CBE and 254.gap with the CBE. git-svn-id: https://llvm.org/svn/llvm-project/llvm/branches/release_21@42081 91177308-0d34-0410-b5e6-96231b3b80d8 Tanya Lattner 13 years ago
2 changed file(s) with 28 addition(s) and 46 deletion(s). Raw diff Collapse all Expand all
659659
660660 /// @returns true if the predicate of this ICmpInst is signed, false otherwise
661661 /// @brief Determine if this instruction's predicate is signed.
662 bool isSignedPredicate() { return isSignedPredicate(getPredicate()); }
662 bool isSignedPredicate() const { return isSignedPredicate(getPredicate()); }
663663
664664 /// @returns true if the predicate provided is signed, false otherwise
665665 /// @brief Determine if the predicate is signed.
137137 void writeOperandRaw(Value *Operand);
138138 void writeOperandInternal(Value *Operand);
139139 void writeOperandWithCast(Value* Operand, unsigned Opcode);
140 void writeOperandWithCast(Value* Operand, ICmpInst::Predicate predicate);
140 void writeOperandWithCast(Value* Operand, const ICmpInst &I);
141141 bool writeInstructionCast(const Instruction &I);
142142
143143 private :
12441244
12451245 // Write the operand with a cast to another type based on the icmp predicate
12461246 // being used.
1247 void CWriter::writeOperandWithCast(Value* Operand, ICmpInst::Predicate predicate) {
1248
1249 // Extract the operand's type, we'll need it.
1250 const Type* OpTy = Operand->getType();
1251
1252 // Indicate whether to do the cast or not.
1253 bool shouldCast = false;
1254
1255 // Indicate whether the cast should be to a signed type or not.
1256 bool castIsSigned = false;
1257
1258 // Based on the Opcode for which this Operand is being written, determine
1259 // the new type to which the operand should be casted by setting the value
1260 // of OpTy. If we change OpTy, also set shouldCast to true.
1261 switch (predicate) {
1262 default:
1263 // for eq and ne, it doesn't matter
1264 break;
1265 case ICmpInst::ICMP_UGT:
1266 case ICmpInst::ICMP_UGE:
1267 case ICmpInst::ICMP_ULT:
1268 case ICmpInst::ICMP_ULE:
1269 shouldCast = true;
1270 break;
1271 case ICmpInst::ICMP_SGT:
1272 case ICmpInst::ICMP_SGE:
1273 case ICmpInst::ICMP_SLT:
1274 case ICmpInst::ICMP_SLE:
1275 shouldCast = true;
1276 castIsSigned = true;
1277 break;
1278 }
1247 void CWriter::writeOperandWithCast(Value* Operand, const ICmpInst &Cmp) {
1248 // This has to do a cast to ensure the operand has the right signedness.
1249 // Also, if the operand is a pointer, we make sure to cast to an integer when
1250 // doing the comparison both for signedness and so that the C compiler doesn't
1251 // optimize things like "p < NULL" to false (p may contain an integer value
1252 // f.e.).
1253 bool shouldCast = Cmp.isRelational();
12791254
12801255 // Write out the casted operand if we should, otherwise just write the
12811256 // operand.
1282 if (shouldCast) {
1283 Out << "((";
1284 if (OpTy->isInteger() && OpTy != Type::Int1Ty)
1285 printSimpleType(Out, OpTy, castIsSigned);
1286 else
1287 printType(Out, OpTy); // not integer, sign doesn't matter
1288 Out << ")";
1257 if (!shouldCast) {
12891258 writeOperand(Operand);
1290 Out << ")";
1291 } else
1292 writeOperand(Operand);
1259 return;
1260 }
1261
1262 // Should this be a signed comparison? If so, convert to signed.
1263 bool castIsSigned = Cmp.isSignedPredicate();
1264
1265 // If the operand was a pointer, convert to a large integer type.
1266 const Type* OpTy = Operand->getType();
1267 if (isa(OpTy))
1268 OpTy = TD->getIntPtrType();
1269
1270 Out << "((";
1271 printSimpleType(Out, OpTy, castIsSigned);
1272 Out << ")";
1273 writeOperand(Operand);
1274 Out << ")";
12931275 }
12941276
12951277 // generateCompilerSpecificCode - This is where we add conditional compilation
22612243 // Certain icmp predicate require the operand to be forced to a specific type
22622244 // so we use writeOperandWithCast here instead of writeOperand. Similarly
22632245 // below for operand 1
2264 writeOperandWithCast(I.getOperand(0), I.getPredicate());
2246 writeOperandWithCast(I.getOperand(0), I);
22652247
22662248 switch (I.getPredicate()) {
22672249 case ICmpInst::ICMP_EQ: Out << " == "; break;
22772259 default: cerr << "Invalid icmp predicate!" << I; abort();
22782260 }
22792261
2280 writeOperandWithCast(I.getOperand(1), I.getPredicate());
2262 writeOperandWithCast(I.getOperand(1), I);
22812263 if (NeedsClosingParens)
22822264 Out << "))";
22832265