llvm.org GIT mirror llvm / ca8cb68
[IR] Add a dedicated FNeg IR Instruction The IEEE-754 Standard makes it clear that fneg(x) and fsub(-0.0, x) are two different operations. The former is a bitwise operation, while the latter is an arithmetic operation. This patch creates a dedicated FNeg IR Instruction to model that behavior. Differential Revision: https://reviews.llvm.org/D53877 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@346774 91177308-0d34-0410-b5e6-96231b3b80d8 Cameron McInally 11 months ago
30 changed file(s) with 596 addition(s) and 84 deletion(s). Raw diff Collapse all Expand all
341341 CST_CODE_INLINEASM = 23, // INLINEASM: [sideeffect|alignstack|
342342 // asmdialect,asmstr,conststr]
343343 CST_CODE_CE_GEP_WITH_INRANGE_INDEX = 24, // [opty, flags, n x operands]
344 CST_CODE_CE_UNOP = 25, // CE_UNOP: [opcode, opval]
344345 };
345346
346347 /// CastOpcodes - These are values used in the bitcode files to encode which
361362 CAST_INTTOPTR = 10,
362363 CAST_BITCAST = 11,
363364 CAST_ADDRSPACECAST = 12
365 };
366
367 /// UnaryOpcodes - These are values used in the bitcode files to encode which
368 /// unop a CST_CODE_CE_UNOP or a XXX refers to. The values of these enums
369 /// have no fixed relation to the LLVM IR enum values. Changing these will
370 /// break compatibility with old files.
371 enum UnaryOpcodes {
372 UNOP_NEG = 0
364373 };
365374
366375 /// BinaryOpcodes - These are values used in the bitcode files to encode which
523532 // 53 is unused.
524533 // 54 is unused.
525534 FUNC_CODE_OPERAND_BUNDLE = 55, // OPERAND_BUNDLE: [tag#, value...]
535 FUNC_CODE_INST_UNOP = 56, // UNOP: [opcode, ty, opval]
526536 };
527537
528538 enum UseListCodes {
299299
300300 bool translateFSub(const User &U, MachineIRBuilder &MIRBuilder);
301301
302 bool translateFNeg(const User &U, MachineIRBuilder &MIRBuilder);
303
302304 bool translateAdd(const User &U, MachineIRBuilder &MIRBuilder) {
303305 return translateBinaryOp(TargetOpcode::G_ADD, U, MIRBuilder);
304306 }
11131113 static Constant *getSelect(Constant *C, Constant *V1, Constant *V2,
11141114 Type *OnlyIfReducedTy = nullptr);
11151115
1116 /// get - Return a unary operator constant expression,
1117 /// folding if possible.
1118 ///
1119 /// \param OnlyIfReducedTy see \a getWithOperands() docs.
1120 static Constant *get(unsigned Opcode, Constant *C1, unsigned Flags = 0,
1121 Type *OnlyIfReducedTy = nullptr);
1122
11161123 /// get - Return a binary or shift operator constant expression,
11171124 /// folding if possible.
11181125 ///
262262 // of instructions...
263263 //
264264 RetTy visitCastInst(CastInst &I) { DELEGATE(UnaryInstruction);}
265 RetTy visitUnaryOperator(UnaryOperator &I) { DELEGATE(UnaryInstruction);}
265266 RetTy visitBinaryOperator(BinaryOperator &I) { DELEGATE(Instruction);}
266267 RetTy visitCmpInst(CmpInst &I) { DELEGATE(Instruction);}
267268 RetTy visitUnaryInstruction(UnaryInstruction &I){ DELEGATE(Instruction);}
2929 #endif
3030 #ifndef LAST_TERM_INST
3131 #define LAST_TERM_INST(num)
32 #endif
33
34 #ifndef FIRST_UNARY_INST
35 #define FIRST_UNARY_INST(num)
36 #endif
37 #ifndef HANDLE_UNARY_INST
38 #ifndef HANDLE_INST
39 #define HANDLE_UNARY_INST(num, opcode, instclass)
40 #else
41 #define HANDLE_UNARY_INST(num, opcode, Class) HANDLE_INST(num, opcode, Class)
42 #endif
43 #endif
44 #ifndef LAST_UNARY_INST
45 #define LAST_UNARY_INST(num)
3246 #endif
3347
3448 #ifndef FIRST_BINARY_INST
122136 HANDLE_TERM_INST (10, CatchSwitch , CatchSwitchInst)
123137 LAST_TERM_INST (10)
124138
139 // Standard unary operators...
140 FIRST_UNARY_INST(11)
141 HANDLE_UNARY_INST(11, FNeg , UnaryOperator)
142 LAST_UNARY_INST(11)
143
125144 // Standard binary operators...
126 FIRST_BINARY_INST(11)
127 HANDLE_BINARY_INST(11, Add , BinaryOperator)
128 HANDLE_BINARY_INST(12, FAdd , BinaryOperator)
129 HANDLE_BINARY_INST(13, Sub , BinaryOperator)
130 HANDLE_BINARY_INST(14, FSub , BinaryOperator)
131 HANDLE_BINARY_INST(15, Mul , BinaryOperator)
132 HANDLE_BINARY_INST(16, FMul , BinaryOperator)
133 HANDLE_BINARY_INST(17, UDiv , BinaryOperator)
134 HANDLE_BINARY_INST(18, SDiv , BinaryOperator)
135 HANDLE_BINARY_INST(19, FDiv , BinaryOperator)
136 HANDLE_BINARY_INST(20, URem , BinaryOperator)
137 HANDLE_BINARY_INST(21, SRem , BinaryOperator)
138 HANDLE_BINARY_INST(22, FRem , BinaryOperator)
145 FIRST_BINARY_INST(12)
146 HANDLE_BINARY_INST(12, Add , BinaryOperator)
147 HANDLE_BINARY_INST(13, FAdd , BinaryOperator)
148 HANDLE_BINARY_INST(14, Sub , BinaryOperator)
149 HANDLE_BINARY_INST(15, FSub , BinaryOperator)
150 HANDLE_BINARY_INST(16, Mul , BinaryOperator)
151 HANDLE_BINARY_INST(17, FMul , BinaryOperator)
152 HANDLE_BINARY_INST(18, UDiv , BinaryOperator)
153 HANDLE_BINARY_INST(19, SDiv , BinaryOperator)
154 HANDLE_BINARY_INST(20, FDiv , BinaryOperator)
155 HANDLE_BINARY_INST(21, URem , BinaryOperator)
156 HANDLE_BINARY_INST(22, SRem , BinaryOperator)
157 HANDLE_BINARY_INST(23, FRem , BinaryOperator)
139158
140159 // Logical operators (integer operands)
141 HANDLE_BINARY_INST(23, Shl , BinaryOperator) // Shift left (logical)
142 HANDLE_BINARY_INST(24, LShr , BinaryOperator) // Shift right (logical)
143 HANDLE_BINARY_INST(25, AShr , BinaryOperator) // Shift right (arithmetic)
144 HANDLE_BINARY_INST(26, And , BinaryOperator)
145 HANDLE_BINARY_INST(27, Or , BinaryOperator)
146 HANDLE_BINARY_INST(28, Xor , BinaryOperator)
147 LAST_BINARY_INST(28)
160 HANDLE_BINARY_INST(24, Shl , BinaryOperator) // Shift left (logical)
161 HANDLE_BINARY_INST(25, LShr , BinaryOperator) // Shift right (logical)
162 HANDLE_BINARY_INST(26, AShr , BinaryOperator) // Shift right (arithmetic)
163 HANDLE_BINARY_INST(27, And , BinaryOperator)
164 HANDLE_BINARY_INST(28, Or , BinaryOperator)
165 HANDLE_BINARY_INST(29, Xor , BinaryOperator)
166 LAST_BINARY_INST(29)
148167
149168 // Memory operators...
150 FIRST_MEMORY_INST(29)
151 HANDLE_MEMORY_INST(29, Alloca, AllocaInst) // Stack management
152 HANDLE_MEMORY_INST(30, Load , LoadInst ) // Memory manipulation instrs
153 HANDLE_MEMORY_INST(31, Store , StoreInst )
154 HANDLE_MEMORY_INST(32, GetElementPtr, GetElementPtrInst)
155 HANDLE_MEMORY_INST(33, Fence , FenceInst )
156 HANDLE_MEMORY_INST(34, AtomicCmpXchg , AtomicCmpXchgInst )
157 HANDLE_MEMORY_INST(35, AtomicRMW , AtomicRMWInst )
158 LAST_MEMORY_INST(35)
169 FIRST_MEMORY_INST(30)
170 HANDLE_MEMORY_INST(30, Alloca, AllocaInst) // Stack management
171 HANDLE_MEMORY_INST(31, Load , LoadInst ) // Memory manipulation instrs
172 HANDLE_MEMORY_INST(32, Store , StoreInst )
173 HANDLE_MEMORY_INST(33, GetElementPtr, GetElementPtrInst)
174 HANDLE_MEMORY_INST(34, Fence , FenceInst )
175 HANDLE_MEMORY_INST(35, AtomicCmpXchg , AtomicCmpXchgInst )
176 HANDLE_MEMORY_INST(36, AtomicRMW , AtomicRMWInst )
177 LAST_MEMORY_INST(36)
159178
160179 // Cast operators ...
161180 // NOTE: The order matters here because CastInst::isEliminableCastPair
162181 // NOTE: (see Instructions.cpp) encodes a table based on this ordering.
163 FIRST_CAST_INST(36)
164 HANDLE_CAST_INST(36, Trunc , TruncInst ) // Truncate integers
165 HANDLE_CAST_INST(37, ZExt , ZExtInst ) // Zero extend integers
166 HANDLE_CAST_INST(38, SExt , SExtInst ) // Sign extend integers
167 HANDLE_CAST_INST(39, FPToUI , FPToUIInst ) // floating point -> UInt
168 HANDLE_CAST_INST(40, FPToSI , FPToSIInst ) // floating point -> SInt
169 HANDLE_CAST_INST(41, UIToFP , UIToFPInst ) // UInt -> floating point
170 HANDLE_CAST_INST(42, SIToFP , SIToFPInst ) // SInt -> floating point
171 HANDLE_CAST_INST(43, FPTrunc , FPTruncInst ) // Truncate floating point
172 HANDLE_CAST_INST(44, FPExt , FPExtInst ) // Extend floating point
173 HANDLE_CAST_INST(45, PtrToInt, PtrToIntInst) // Pointer -> Integer
174 HANDLE_CAST_INST(46, IntToPtr, IntToPtrInst) // Integer -> Pointer
175 HANDLE_CAST_INST(47, BitCast , BitCastInst ) // Type cast
176 HANDLE_CAST_INST(48, AddrSpaceCast, AddrSpaceCastInst) // addrspace cast
177 LAST_CAST_INST(48)
178
179 FIRST_FUNCLETPAD_INST(49)
180 HANDLE_FUNCLETPAD_INST(49, CleanupPad, CleanupPadInst)
181 HANDLE_FUNCLETPAD_INST(50, CatchPad , CatchPadInst)
182 LAST_FUNCLETPAD_INST(50)
182 FIRST_CAST_INST(37)
183 HANDLE_CAST_INST(37, Trunc , TruncInst ) // Truncate integers
184 HANDLE_CAST_INST(38, ZExt , ZExtInst ) // Zero extend integers
185 HANDLE_CAST_INST(39, SExt , SExtInst ) // Sign extend integers
186 HANDLE_CAST_INST(40, FPToUI , FPToUIInst ) // floating point -> UInt
187 HANDLE_CAST_INST(41, FPToSI , FPToSIInst ) // floating point -> SInt
188 HANDLE_CAST_INST(42, UIToFP , UIToFPInst ) // UInt -> floating point
189 HANDLE_CAST_INST(43, SIToFP , SIToFPInst ) // SInt -> floating point
190 HANDLE_CAST_INST(44, FPTrunc , FPTruncInst ) // Truncate floating point
191 HANDLE_CAST_INST(45, FPExt , FPExtInst ) // Extend floating point
192 HANDLE_CAST_INST(46, PtrToInt, PtrToIntInst) // Pointer -> Integer
193 HANDLE_CAST_INST(47, IntToPtr, IntToPtrInst) // Integer -> Pointer
194 HANDLE_CAST_INST(48, BitCast , BitCastInst ) // Type cast
195 HANDLE_CAST_INST(49, AddrSpaceCast, AddrSpaceCastInst) // addrspace cast
196 LAST_CAST_INST(49)
197
198 FIRST_FUNCLETPAD_INST(50)
199 HANDLE_FUNCLETPAD_INST(50, CleanupPad, CleanupPadInst)
200 HANDLE_FUNCLETPAD_INST(51, CatchPad , CatchPadInst)
201 LAST_FUNCLETPAD_INST(51)
183202
184203 // Other operators...
185 FIRST_OTHER_INST(51)
186 HANDLE_OTHER_INST(51, ICmp , ICmpInst ) // Integer comparison instruction
187 HANDLE_OTHER_INST(52, FCmp , FCmpInst ) // Floating point comparison instr.
188 HANDLE_OTHER_INST(53, PHI , PHINode ) // PHI node instruction
189 HANDLE_OTHER_INST(54, Call , CallInst ) // Call a function
190 HANDLE_OTHER_INST(55, Select , SelectInst ) // select instruction
191 HANDLE_USER_INST (56, UserOp1, Instruction) // May be used internally in a pass
192 HANDLE_USER_INST (57, UserOp2, Instruction) // Internal to passes only
193 HANDLE_OTHER_INST(58, VAArg , VAArgInst ) // vaarg instruction
194 HANDLE_OTHER_INST(59, ExtractElement, ExtractElementInst)// extract from vector
195 HANDLE_OTHER_INST(60, InsertElement, InsertElementInst) // insert into vector
196 HANDLE_OTHER_INST(61, ShuffleVector, ShuffleVectorInst) // shuffle two vectors.
197 HANDLE_OTHER_INST(62, ExtractValue, ExtractValueInst)// extract from aggregate
198 HANDLE_OTHER_INST(63, InsertValue, InsertValueInst) // insert into aggregate
199 HANDLE_OTHER_INST(64, LandingPad, LandingPadInst) // Landing pad instruction.
200 LAST_OTHER_INST(64)
204 FIRST_OTHER_INST(52)
205 HANDLE_OTHER_INST(52, ICmp , ICmpInst ) // Integer comparison instruction
206 HANDLE_OTHER_INST(53, FCmp , FCmpInst ) // Floating point comparison instr.
207 HANDLE_OTHER_INST(54, PHI , PHINode ) // PHI node instruction
208 HANDLE_OTHER_INST(55, Call , CallInst ) // Call a function
209 HANDLE_OTHER_INST(56, Select , SelectInst ) // select instruction
210 HANDLE_USER_INST (57, UserOp1, Instruction) // May be used internally in a pass
211 HANDLE_USER_INST (58, UserOp2, Instruction) // Internal to passes only
212 HANDLE_OTHER_INST(59, VAArg , VAArgInst ) // vaarg instruction
213 HANDLE_OTHER_INST(60, ExtractElement, ExtractElementInst)// extract from vector
214 HANDLE_OTHER_INST(61, InsertElement, InsertElementInst) // insert into vector
215 HANDLE_OTHER_INST(62, ShuffleVector, ShuffleVectorInst) // shuffle two vectors.
216 HANDLE_OTHER_INST(63, ExtractValue, ExtractValueInst)// extract from aggregate
217 HANDLE_OTHER_INST(64, InsertValue, InsertValueInst) // insert into aggregate
218 HANDLE_OTHER_INST(65, LandingPad, LandingPadInst) // Landing pad instruction.
219 LAST_OTHER_INST(65)
201220
202221 #undef FIRST_TERM_INST
203222 #undef HANDLE_TERM_INST
204223 #undef LAST_TERM_INST
205224
225 #undef FIRST_UNARY_INST
226 #undef HANDLE_UNARY_INST
227 #undef LAST_UNARY_INST
228
206229 #undef FIRST_BINARY_INST
207230 #undef HANDLE_BINARY_INST
208231 #undef LAST_BINARY_INST
126126
127127 const char *getOpcodeName() const { return getOpcodeName(getOpcode()); }
128128 bool isTerminator() const { return isTerminator(getOpcode()); }
129 bool isUnaryOp() const { return isUnaryOp(getOpcode()); }
129130 bool isBinaryOp() const { return isBinaryOp(getOpcode()); }
130131 bool isIntDivRem() const { return isIntDivRem(getOpcode()); }
131132 bool isShift() { return isShift(getOpcode()); }
141142 return OpCode >= TermOpsBegin && OpCode < TermOpsEnd;
142143 }
143144
145 static inline bool isUnaryOp(unsigned Opcode) {
146 return Opcode >= UnaryOpsBegin && Opcode < UnaryOpsEnd;
147 }
144148 static inline bool isBinaryOp(unsigned Opcode) {
145149 return Opcode >= BinaryOpsBegin && Opcode < BinaryOpsEnd;
146150 }
661665 #include "llvm/IR/Instruction.def"
662666 };
663667
668 enum UnaryOps {
669 #define FIRST_UNARY_INST(N) UnaryOpsBegin = N,
670 #define HANDLE_UNARY_INST(N, OPC, CLASS) OPC = N,
671 #define LAST_UNARY_INST(N) UnaryOpsEnd = N+1
672 #include "llvm/IR/Instruction.def"
673 };
674
664675 enum BinaryOps {
665676 #define FIRST_BINARY_INST(N) BinaryOpsBegin = N,
666677 #define HANDLE_BINARY_INST(N, OPC, CLASS) OPC = N,
11031103 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrInst, Value)
11041104
11051105 //===----------------------------------------------------------------------===//
1106 // UnaryOperator Class
1107 //===----------------------------------------------------------------------===//
1108
1109 /// a unary instruction
1110 class UnaryOperator : public UnaryInstruction {
1111 void AssertOK();
1112
1113 protected:
1114 UnaryOperator(UnaryOps iType, Value *S, Type *Ty,
1115 const Twine &Name, Instruction *InsertBefore);
1116 UnaryOperator(UnaryOps iType, Value *S, Type *Ty,
1117 const Twine &Name, BasicBlock *InsertAtEnd);
1118
1119 // Note: Instruction needs to be a friend here to call cloneImpl.
1120 friend class Instruction;
1121
1122 UnaryOperator *cloneImpl() const;
1123
1124 public:
1125
1126 /// Construct a unary instruction, given the opcode and an operand.
1127 /// Optionally (if InstBefore is specified) insert the instruction
1128 /// into a BasicBlock right before the specified instruction. The specified
1129 /// Instruction is allowed to be a dereferenced end iterator.
1130 ///
1131 static UnaryOperator *Create(UnaryOps Op, Value *S,
1132 const Twine &Name = Twine(),
1133 Instruction *InsertBefore = nullptr);
1134
1135 /// Construct a unary instruction, given the opcode and an operand.
1136 /// Also automatically insert this instruction to the end of the
1137 /// BasicBlock specified.
1138 ///
1139 static UnaryOperator *Create(UnaryOps Op, Value *S,
1140 const Twine &Name,
1141 BasicBlock *InsertAtEnd);
1142
1143 /// These methods just forward to Create, and are useful when you
1144 /// statically know what type of instruction you're going to create. These
1145 /// helpers just save some typing.
1146 #define HANDLE_UNARY_INST(N, OPC, CLASS) \
1147 static UnaryInstruction *Create##OPC(Value *V, \
1148 const Twine &Name = "") {\
1149 return Create(Instruction::OPC, V, Name);\
1150 }
1151 #include "llvm/IR/Instruction.def"
1152 #define HANDLE_UNARY_INST(N, OPC, CLASS) \
1153 static UnaryInstruction *Create##OPC(Value *V, \
1154 const Twine &Name, BasicBlock *BB) {\
1155 return Create(Instruction::OPC, V, Name, BB);\
1156 }
1157 #include "llvm/IR/Instruction.def"
1158 #define HANDLE_UNARY_INST(N, OPC, CLASS) \
1159 static UnaryInstruction *Create##OPC(Value *V, \
1160 const Twine &Name, Instruction *I) {\
1161 return Create(Instruction::OPC, V, Name, I);\
1162 }
1163 #include "llvm/IR/Instruction.def"
1164
1165 UnaryOps getOpcode() const {
1166 return static_cast(Instruction::getOpcode());
1167 }
1168 };
1169
1170 //===----------------------------------------------------------------------===//
11061171 // ICmpInst Class
11071172 //===----------------------------------------------------------------------===//
11081173
5353 * @{
5454 */
5555
56 /// External users depend on the following values being stable. It is not safe
57 /// to reorder them.
5658 typedef enum {
5759 /* Terminator Instructions */
5860 LLVMRet = 1,
6264 LLVMInvoke = 5,
6365 /* removed 6 due to API changes */
6466 LLVMUnreachable = 7,
67
68 /* Standard Unary Operators */
69 LLVMFNeg = 66,
6570
6671 /* Standard Binary Operators */
6772 LLVMAdd = 8,
822822 } \
823823 } while (false)
824824
825 INSTKEYWORD(fneg, FNeg);
826
825827 INSTKEYWORD(add, Add); INSTKEYWORD(fadd, FAdd);
826828 INSTKEYWORD(sub, Sub); INSTKEYWORD(fsub, FSub);
827829 INSTKEYWORD(mul, Mul); INSTKEYWORD(fmul, FMul);
32943294 ID.Kind = ValID::t_Constant;
32953295 return false;
32963296 }
3297
3297
3298 // Unary Operators.
3299 case lltok::kw_fneg: {
3300 unsigned Opc = Lex.getUIntVal();
3301 Constant *Val;
3302 Lex.Lex();
3303 if (ParseToken(lltok::lparen, "expected '(' in unary constantexpr") ||
3304 ParseGlobalTypeAndValue(Val) ||
3305 ParseToken(lltok::rparen, "expected ')' in unary constantexpr"))
3306 return true;
3307
3308 // Check that the type is valid for the operator.
3309 switch (Opc) {
3310 case Instruction::FNeg:
3311 if (!Val->getType()->isFPOrFPVectorTy())
3312 return Error(ID.Loc, "constexpr requires fp operands");
3313 break;
3314 default: llvm_unreachable("Unknown unary operator!");
3315 }
3316 unsigned Flags = 0;
3317 Constant *C = ConstantExpr::get(Opc, Val, Flags);
3318 ID.ConstantVal = C;
3319 ID.Kind = ValID::t_Constant;
3320 return false;
3321 }
32983322 // Binary Operators.
32993323 case lltok::kw_add:
33003324 case lltok::kw_fadd:
54915515 case lltok::kw_catchswitch: return ParseCatchSwitch(Inst, PFS);
54925516 case lltok::kw_catchpad: return ParseCatchPad(Inst, PFS);
54935517 case lltok::kw_cleanuppad: return ParseCleanupPad(Inst, PFS);
5518 // Unary Operators.
5519 case lltok::kw_fneg: {
5520 FastMathFlags FMF = EatFastMathFlagsIfPresent();
5521 int Res = ParseUnaryOp(Inst, PFS, KeywordVal, 2);
5522 if (Res != 0)
5523 return Res;
5524 if (FMF.any())
5525 Inst->setFastMathFlags(FMF);
5526 return false;
5527 }
54945528 // Binary Operators.
54955529 case lltok::kw_add:
54965530 case lltok::kw_sub:
60636097 }
60646098
60656099 //===----------------------------------------------------------------------===//
6100 // Unary Operators.
6101 //===----------------------------------------------------------------------===//
6102
6103 /// ParseUnaryOp
6104 /// ::= UnaryOp TypeAndValue ',' Value
6105 ///
6106 /// If OperandType is 0, then any FP or integer operand is allowed. If it is 1,
6107 /// then any integer operand is allowed, if it is 2, any fp operand is allowed.
6108 bool LLParser::ParseUnaryOp(Instruction *&Inst, PerFunctionState &PFS,
6109 unsigned Opc, unsigned OperandType) {
6110 LocTy Loc; Value *LHS;
6111 if (ParseTypeAndValue(LHS, Loc, PFS))
6112 return true;
6113
6114 bool Valid;
6115 switch (OperandType) {
6116 default: llvm_unreachable("Unknown operand type!");
6117 case 0: // int or FP.
6118 Valid = LHS->getType()->isIntOrIntVectorTy() ||
6119 LHS->getType()->isFPOrFPVectorTy();
6120 break;
6121 case 1:
6122 Valid = LHS->getType()->isIntOrIntVectorTy();
6123 break;
6124 case 2:
6125 Valid = LHS->getType()->isFPOrFPVectorTy();
6126 break;
6127 }
6128
6129 if (!Valid)
6130 return Error(Loc, "invalid operand type for instruction");
6131
6132 Inst = UnaryOperator::Create((Instruction::UnaryOps)Opc, LHS);
6133 return false;
6134 }
6135
6136 //===----------------------------------------------------------------------===//
60666137 // Binary Operators.
60676138 //===----------------------------------------------------------------------===//
60686139
570570 bool ParseCatchPad(Instruction *&Inst, PerFunctionState &PFS);
571571 bool ParseCleanupPad(Instruction *&Inst, PerFunctionState &PFS);
572572
573 bool ParseUnaryOp(Instruction *&Inst, PerFunctionState &PFS, unsigned Opc,
574 unsigned OperandType);
573575 bool ParseArithmetic(Instruction *&Inst, PerFunctionState &PFS, unsigned Opc,
574576 unsigned OperandType);
575577 bool ParseLogical(Instruction *&Inst, PerFunctionState &PFS, unsigned Opc);
269269 kw_umin,
270270
271271 // Instruction Opcodes (Opcode in UIntVal).
272 kw_fneg,
272273 kw_add,
273274 kw_fadd,
274275 kw_sub,
960960 case bitc::CAST_INTTOPTR: return Instruction::IntToPtr;
961961 case bitc::CAST_BITCAST : return Instruction::BitCast;
962962 case bitc::CAST_ADDRSPACECAST: return Instruction::AddrSpaceCast;
963 }
964 }
965
966 static int getDecodedUnaryOpcode(unsigned Val, Type *Ty) {
967 bool IsFP = Ty->isFPOrFPVectorTy();
968 // UnOps are only valid for int/fp or vector of int/fp types
969 if (!IsFP && !Ty->isIntOrIntVectorTy())
970 return -1;
971
972 switch (Val) {
973 default:
974 return -1;
975 case bitc::UNOP_NEG:
976 return IsFP ? Instruction::FNeg : -1;
963977 }
964978 }
965979
23162330 }
23172331 break;
23182332 }
2333 case bitc::CST_CODE_CE_UNOP: { // CE_UNOP: [opcode, opval]
2334 if (Record.size() < 2)
2335 return error("Invalid record");
2336 int Opc = getDecodedUnaryOpcode(Record[0], CurTy);
2337 if (Opc < 0) {
2338 V = UndefValue::get(CurTy); // Unknown unop.
2339 } else {
2340 Constant *LHS = ValueList.getConstantFwdRef(Record[1], CurTy);
2341 unsigned Flags = 0;
2342 V = ConstantExpr::get(Opc, LHS, Flags);
2343 }
2344 break;
2345 }
23192346 case bitc::CST_CODE_CE_BINOP: { // CE_BINOP: [opcode, opval, opval]
23202347 if (Record.size() < 3)
23212348 return error("Invalid record");
35343561 I = nullptr;
35353562 continue;
35363563 }
3537
3564 case bitc::FUNC_CODE_INST_UNOP: { // UNOP: [opval, ty, opcode]
3565 unsigned OpNum = 0;
3566 Value *LHS;
3567 if (getValueTypePair(Record, OpNum, NextValueNo, LHS) ||
3568 OpNum+1 > Record.size())
3569 return error("Invalid record");
3570
3571 int Opc = getDecodedUnaryOpcode(Record[OpNum++], LHS->getType());
3572 if (Opc == -1)
3573 return error("Invalid record");
3574 I = UnaryOperator::Create((Instruction::UnaryOps)Opc, LHS);
3575 InstructionList.push_back(I);
3576 if (OpNum < Record.size()) {
3577 if (isa(I)) {
3578 FastMathFlags FMF = getDecodedFastMathFlags(Record[OpNum]);
3579 if (FMF.any())
3580 I->setFastMathFlags(FMF);
3581 }
3582 }
3583 break;
3584 }
35383585 case bitc::FUNC_CODE_INST_BINOP: { // BINOP: [opval, ty, opval, opcode]
35393586 unsigned OpNum = 0;
35403587 Value *LHS, *RHS;
111111
112112 // FUNCTION_BLOCK abbrev id's.
113113 FUNCTION_INST_LOAD_ABBREV = bitc::FIRST_APPLICATION_ABBREV,
114 FUNCTION_INST_UNOP_ABBREV,
115 FUNCTION_INST_UNOP_FLAGS_ABBREV,
114116 FUNCTION_INST_BINOP_ABBREV,
115117 FUNCTION_INST_BINOP_FLAGS_ABBREV,
116118 FUNCTION_INST_CAST_ABBREV,
509511 case Instruction::IntToPtr: return bitc::CAST_INTTOPTR;
510512 case Instruction::BitCast : return bitc::CAST_BITCAST;
511513 case Instruction::AddrSpaceCast: return bitc::CAST_ADDRSPACECAST;
514 }
515 }
516
517 static unsigned getEncodedUnaryOpcode(unsigned Opcode) {
518 switch (Opcode) {
519 default: llvm_unreachable("Unknown binary instruction!");
520 case Instruction::FNeg: return bitc::UNOP_NEG;
512521 }
513522 }
514523
23832392 Record.push_back(Flags);
23842393 }
23852394 break;
2395 case Instruction::FNeg: {
2396 assert(CE->getNumOperands() == 1 && "Unknown constant expr!");
2397 Code = bitc::CST_CODE_CE_UNOP;
2398 Record.push_back(getEncodedUnaryOpcode(CE->getOpcode()));
2399 Record.push_back(VE.getValueID(C->getOperand(0)));
2400 uint64_t Flags = getOptimizationFlags(CE);
2401 if (Flags != 0)
2402 Record.push_back(Flags);
2403 break;
2404 }
23862405 case Instruction::GetElementPtr: {
23872406 Code = bitc::CST_CODE_CE_GEP;
23882407 const auto *GO = cast(C);
25552574 }
25562575 }
25572576 break;
2558
2577 case Instruction::FNeg: {
2578 Code = bitc::FUNC_CODE_INST_UNOP;
2579 if (!pushValueAndType(I.getOperand(0), InstID, Vals))
2580 AbbrevToUse = FUNCTION_INST_UNOP_ABBREV;
2581 Vals.push_back(getEncodedUnaryOpcode(I.getOpcode()));
2582 uint64_t Flags = getOptimizationFlags(&I);
2583 if (Flags != 0) {
2584 if (AbbrevToUse == FUNCTION_INST_UNOP_ABBREV)
2585 AbbrevToUse = FUNCTION_INST_UNOP_FLAGS_ABBREV;
2586 Vals.push_back(Flags);
2587 }
2588 break;
2589 }
25592590 case Instruction::GetElementPtr: {
25602591 Code = bitc::FUNC_CODE_INST_GEP;
25612592 AbbrevToUse = FUNCTION_INST_GEP_ABBREV;
32163247 FUNCTION_INST_LOAD_ABBREV)
32173248 llvm_unreachable("Unexpected abbrev ordering!");
32183249 }
3250 { // INST_UNOP abbrev for FUNCTION_BLOCK.
3251 auto Abbv = std::make_shared();
3252 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_UNOP));
3253 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LHS
3254 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
3255 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
3256 FUNCTION_INST_UNOP_ABBREV)
3257 llvm_unreachable("Unexpected abbrev ordering!");
3258 }
3259 { // INST_UNOP_FLAGS abbrev for FUNCTION_BLOCK.
3260 auto Abbv = std::make_shared();
3261 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_UNOP));
3262 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LHS
3263 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
3264 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8)); // flags
3265 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
3266 FUNCTION_INST_UNOP_FLAGS_ABBREV)
3267 llvm_unreachable("Unexpected abbrev ordering!");
3268 }
32193269 { // INST_BINOP abbrev for FUNCTION_BLOCK.
32203270 auto Abbv = std::make_shared();
32213271 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_BINOP));
329329 return translateBinaryOp(TargetOpcode::G_FSUB, U, MIRBuilder);
330330 }
331331
332 bool IRTranslator::translateFNeg(const User &U, MachineIRBuilder &MIRBuilder) {
333 MIRBuilder.buildInstr(TargetOpcode::G_FNEG)
334 .addDef(getOrCreateVReg(U))
335 .addUse(getOrCreateVReg(*U.getOperand(1)));
336 return true;
337 }
338
332339 bool IRTranslator::translateCompare(const User &U,
333340 MachineIRBuilder &MIRBuilder) {
334341 const CmpInst *CI = dyn_cast(&U);
27982798 }
27992799 }
28002800 return ReduxExtracted;
2801 }
2802
2803 void SelectionDAGBuilder::visitUnary(const User &I, unsigned Opcode) {
2804 SDNodeFlags Flags;
2805
2806 SDValue Op = getValue(I.getOperand(0));
2807 SDValue UnNodeValue = DAG.getNode(Opcode, getCurSDLoc(), Op.getValueType(),
2808 Op, Flags);
2809 setValue(&I, UnNodeValue);
28012810 }
28022811
28032812 void SelectionDAGBuilder::visitBinary(const User &I, unsigned Opcode) {
853853 void visitInvoke(const InvokeInst &I);
854854 void visitResume(const ResumeInst &I);
855855
856 void visitUnary(const User &I, unsigned Opcode);
857 void visitFNeg(const User &I) { visitUnary(I, ISD::FNEG); }
858
856859 void visitBinary(const User &I, unsigned Opcode);
857860 void visitShift(const User &I, unsigned Opcode);
858861 void visitAdd(const User &I) { visitBinary(I, ISD::ADD); }
14501450 case CatchPad: return 0;
14511451 case CatchSwitch: return 0;
14521452 case CleanupPad: return 0;
1453 case FNeg: return ISD::FNEG;
14531454 case Add: return ISD::ADD;
14541455 case FAdd: return ISD::FADD;
14551456 case Sub: return ISD::SUB;
17771777 C = getBitCast(C, MidTy);
17781778 }
17791779 return getFoldedCast(Instruction::AddrSpaceCast, C, DstTy, OnlyIfReduced);
1780 }
1781
1782 Constant *ConstantExpr::get(unsigned Opcode, Constant *C, unsigned Flags,
1783 Type *OnlyIfReducedTy) {
1784 // Check the operands for consistency first.
1785 assert(Instruction::isUnaryOp(Opcode) &&
1786 "Invalid opcode in unary constant expression");
1787
1788 #ifndef NDEBUG
1789 switch (Opcode) {
1790 case Instruction::FNeg:
1791 assert(C->getType()->isFPOrFPVectorTy() &&
1792 "Tried to create a floating-point operation on a "
1793 "non-floating-point type!");
1794 break;
1795 default:
1796 break;
1797 }
1798 #endif
1799
1800 // TODO: Try to constant fold operation.
1801
1802 if (OnlyIfReducedTy == C->getType())
1803 return nullptr;
1804
1805 Constant *ArgVec[] = { C };
1806 ConstantExprKeyType Key(Opcode, ArgVec, 0, Flags);
1807
1808 LLVMContextImpl *pImpl = C->getContext().pImpl;
1809 return pImpl->ExprConstants.getOrCreate(C->getType(), Key);
17801810 }
17811811
17821812 Constant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2,
528528 ConstantExpr *create(TypeClass *Ty) const {
529529 switch (Opcode) {
530530 default:
531 if (Instruction::isCast(Opcode))
531 if (Instruction::isCast(Opcode) ||
532 (Opcode >= Instruction::UnaryOpsBegin &&
533 Opcode < Instruction::UnaryOpsEnd))
532534 return new UnaryConstantExpr(Opcode, Ops[0], Ty);
533535 if ((Opcode >= Instruction::BinaryOpsBegin &&
534536 Opcode < Instruction::BinaryOpsEnd))
301301 case CatchRet: return "catchret";
302302 case CatchPad: return "catchpad";
303303 case CatchSwitch: return "catchswitch";
304
305 // Standard unary operators...
306 case FNeg: return "fneg";
304307
305308 // Standard binary operators...
306309 case Add: return "add";
19531953 Agg = cast(Agg)->getTypeAtIndex(Index);
19541954 }
19551955 return const_cast(Agg);
1956 }
1957
1958 //===----------------------------------------------------------------------===//
1959 // UnaryOperator Class
1960 //===----------------------------------------------------------------------===//
1961
1962 UnaryOperator::UnaryOperator(UnaryOps iType, Value *S,
1963 Type *Ty, const Twine &Name,
1964 Instruction *InsertBefore)
1965 : UnaryInstruction(Ty, iType, S, InsertBefore) {
1966 Op<0>() = S;
1967 setName(Name);
1968 AssertOK();
1969 }
1970
1971 UnaryOperator::UnaryOperator(UnaryOps iType, Value *S,
1972 Type *Ty, const Twine &Name,
1973 BasicBlock *InsertAtEnd)
1974 : UnaryInstruction(Ty, iType, S, InsertAtEnd) {
1975 Op<0>() = S;
1976 setName(Name);
1977 AssertOK();
1978 }
1979
1980 UnaryOperator *UnaryOperator::Create(UnaryOps Op, Value *S,
1981 const Twine &Name,
1982 Instruction *InsertBefore) {
1983 return new UnaryOperator(Op, S, S->getType(), Name, InsertBefore);
1984 }
1985
1986 UnaryOperator *UnaryOperator::Create(UnaryOps Op, Value *S,
1987 const Twine &Name,
1988 BasicBlock *InsertAtEnd) {
1989 UnaryOperator *Res = Create(Op, S, Name);
1990 InsertAtEnd->getInstList().push_back(Res);
1991 return Res;
1992 }
1993
1994 void UnaryOperator::AssertOK() {
1995 Value *LHS = getOperand(0);
1996 (void)LHS; // Silence warnings.
1997 #ifndef NDEBUG
1998 switch (getOpcode()) {
1999 case FNeg:
2000 assert(getType() == LHS->getType() &&
2001 "Unary operation should return same type as operand!");
2002 assert(getType()->isFPOrFPVectorTy() &&
2003 "Tried to create a floating-point operation on a "
2004 "non-floating-point type!");
2005 break;
2006 default: llvm_unreachable("Invalid opcode provided");
2007 }
2008 #endif
19562009 }
19572010
19582011 //===----------------------------------------------------------------------===//
36963749 return new (getNumOperands()) GetElementPtrInst(*this);
36973750 }
36983751
3752 UnaryOperator *UnaryOperator::cloneImpl() const {
3753 return Create(getOpcode(), Op<0>());
3754 }
3755
36993756 BinaryOperator *BinaryOperator::cloneImpl() const {
37003757 return Create(getOpcode(), Op<0>(), Op<1>());
37013758 }
442442 void visitBitCastInst(BitCastInst &I);
443443 void visitAddrSpaceCastInst(AddrSpaceCastInst &I);
444444 void visitPHINode(PHINode &PN);
445 void visitUnaryOperator(UnaryOperator &U);
445446 void visitBinaryOperator(BinaryOperator &B);
446447 void visitICmpInst(ICmpInst &IC);
447448 void visitFCmpInst(FCmpInst &FC);
29872988 &II);
29882989
29892990 visitTerminator(II);
2991 }
2992
2993 /// visitUnaryOperator - Check the argument to the unary operator.
2994 ///
2995 void Verifier::visitUnaryOperator(UnaryOperator &U) {
2996 Assert(U.getType() == U.getOperand(0)->getType(),
2997 "Unary operators must have same type for"
2998 "operands and result!",
2999 &U);
3000
3001 switch (U.getOpcode()) {
3002 // Check that floating-point arithmetic operators are only used with
3003 // floating-point operands.
3004 case Instruction::FNeg:
3005 Assert(U.getType()->isFPOrFPVectorTy(),
3006 "FNeg operator only works with float types!", &U);
3007 break;
3008 default:
3009 llvm_unreachable("Unknown UnaryOperator opcode!");
3010 }
3011
3012 visitInstruction(U);
29903013 }
29913014
29923015 /// visitBinaryOperator - Check that both arguments to the binary operator are
3737 %e = frem float %x, %y
3838 ; CHECK: %e_vec = frem <3 x float> %vec, %vec
3939 %e_vec = frem <3 x float> %vec, %vec
40 ; CHECK: ret float %e
41 ret float %e
40 ; CHECK: %f = fneg float %x
41 %f = fneg float %x
42 ; CHECK: %f_vec = fneg <3 x float> %vec
43 %f_vec = fneg <3 x float> %vec
44 ; CHECK: ret float %f
45 ret float %f
4246 }
4347
4448 ; CHECK: no_nan
7175 %e = frem nnan float %x, %y
7276 ; CHECK: %e_vec = frem nnan <3 x float> %vec, %vec
7377 %e_vec = frem nnan <3 x float> %vec, %vec
74 ; CHECK: ret float %e
75 ret float %e
78 ; CHECK: %f = fneg nnan float %x
79 %f = fneg nnan float %x
80 ; CHECK: %f_vec = fneg nnan <3 x float> %vec
81 %f_vec = fneg nnan <3 x float> %vec
82 ; CHECK: ret float %f
83 ret float %f
7684 }
7785
7886 ; CHECK: @contract(
173181 %e = frem nnan nsz float %x, %y
174182 ; CHECK: %e_vec = frem nnan <3 x float> %vec, %vec
175183 %e_vec = frem nnan <3 x float> %vec, %vec
176 ; CHECK: ret float %e
177 ret float %e
184 ; CHECK: %f = fneg nnan nsz float %x
185 %f = fneg nnan nsz float %x
186 ; CHECK: %f_vec = fneg fast <3 x float> %vec
187 %f_vec = fneg fast <3 x float> %vec
188 ; CHECK: ret float %f
189 ret float %f
178190 }
761761 }
762762
763763 ;; Fast Math Flags
764 define void @fastmathflags(float %op1, float %op2) {
764 define void @fastmathflags_unop(float %op1) {
765 %f.nnan = fneg nnan float %op1
766 ; CHECK: %f.nnan = fneg nnan float %op1
767 %f.ninf = fneg ninf float %op1
768 ; CHECK: %f.ninf = fneg ninf float %op1
769 %f.nsz = fneg nsz float %op1
770 ; CHECK: %f.nsz = fneg nsz float %op1
771 %f.arcp = fneg arcp float %op1
772 ; CHECK: %f.arcp = fneg arcp float %op1
773 %f.contract = fneg contract float %op1
774 ; CHECK: %f.contract = fneg contract float %op1
775 %f.afn = fneg afn float %op1
776 ; CHECK: %f.afn = fneg afn float %op1
777 %f.reassoc = fneg reassoc float %op1
778 ; CHECK: %f.reassoc = fneg reassoc float %op1
779 %f.fast = fneg fast float %op1
780 ; CHECK: %f.fast = fneg fast float %op1
781 ret void
782 }
783
784 define void @fastmathflags_binops(float %op1, float %op2) {
765785 %f.nnan = fadd nnan float %op1, %op2
766786 ; CHECK: %f.nnan = fadd nnan float %op1, %op2
767787 %f.ninf = fadd ninf float %op1, %op2
9941014
9951015 continue:
9961016 ret i32 0
1017 }
1018
1019 ; Instructions -- Unary Operations
1020 define void @instructions.unops(double %op1) {
1021 fneg double %op1
1022 ; CHECK: fneg double %op1
1023 ret void
9971024 }
9981025
9991026 ; Instructions -- Binary Operations
11 ; relative IDs.
22 ; RUN: llvm-as < %s | llvm-bcanalyzer -dump | FileCheck %s
33 ; RUN: verify-uselistorder < %s
4
5 ; CHECK: FUNCTION_BLOCK
6 ; CHECK: INST_UNOP {{.*}}op0=1
7 ; CHECK: INST_RET {{.*}}op0=1
8 define double @test_float_unops(double %a) nounwind {
9 %1 = fneg double %a
10 ret double %1
11 }
12
413
514 ; CHECK: FUNCTION_BLOCK
615 ; CHECK: INST_BINOP {{.*}}op0=1 op1=1
66
77 define float @fnegf(float %X) {
88 %Y = fsub float -0.000000e+00, %X ; [#uses=1]
9 ret float %Y
10 }
11
12 define double @real_fneg(double %X) {
13 %Y = fneg double %X ; [#uses=1]
14 ret double %Y
15 }
16
17 define double @real_fneg_constant() {
18 %Y = fneg double -2.0 ; [#uses=1]
19 ret double %Y
20 }
21
22 define float @real_fnegf(float %X) {
23 %Y = fneg float %X ; [#uses=1]
924 ret float %Y
1025 }
1126
126126 ret <4 x float> %r
127127 }
128128
129 define <4 x float> @fneg(<4 x float> %Q) nounwind {
130 ; X32-SSE-LABEL: fneg:
131 ; X32-SSE: # %bb.0:
132 ; X32-SSE-NEXT: xorps {{\.LCPI.*}}, %xmm0
133 ; X32-SSE-NEXT: retl
134 ;
135 ; X64-SSE-LABEL: fneg:
136 ; X64-SSE: # %bb.0:
137 ; X64-SSE-NEXT: xorps {{.*}}(%rip), %xmm0
138 ; X64-SSE-NEXT: retq
139 %tmp = fneg <4 x float> %Q
140 ret <4 x float> %tmp
141 }
142
143
6262 resume { i8*, i32 } zeroinitializer
6363 }
6464
65 define i8 @call_with_same_range() {
66 ; CHECK-LABEL: @call_with_same_range
67 ; CHECK: tail call i8 @call_with_range
68 bitcast i8 0 to i8
69 %out = call i8 @dummy(), !range !0
70 ret i8 %out
71 }
72
6573 define i8 @invoke_with_same_range() personality i8* undef {
6674 ; CHECK-LABEL: @invoke_with_same_range()
6775 ; CHECK: tail call i8 @invoke_with_range()
7583 resume { i8*, i32 } zeroinitializer
7684 }
7785
78 define i8 @call_with_same_range() {
79 ; CHECK-LABEL: @call_with_same_range
80 ; CHECK: tail call i8 @call_with_range
81 bitcast i8 0 to i8
82 %out = call i8 @dummy(), !range !0
83 ret i8 %out
84 }
85
8686
8787
8888 declare i8 @dummy();
246246 STRINGIFY_CODE(CST_CODE, CE_CMP)
247247 STRINGIFY_CODE(CST_CODE, INLINEASM)
248248 STRINGIFY_CODE(CST_CODE, CE_SHUFVEC_EX)
249 STRINGIFY_CODE(CST_CODE, CE_UNOP)
249250 case bitc::CST_CODE_BLOCKADDRESS: return "CST_CODE_BLOCKADDRESS";
250251 STRINGIFY_CODE(CST_CODE, DATA)
251252 }
266267 STRINGIFY_CODE(FUNC_CODE, INST_BR)
267268 STRINGIFY_CODE(FUNC_CODE, INST_SWITCH)
268269 STRINGIFY_CODE(FUNC_CODE, INST_INVOKE)
270 STRINGIFY_CODE(FUNC_CODE, INST_UNOP)
269271 STRINGIFY_CODE(FUNC_CODE, INST_UNREACHABLE)
270272 STRINGIFY_CODE(FUNC_CODE, INST_CLEANUPRET)
271273 STRINGIFY_CODE(FUNC_CODE, INST_CATCHRET)