blob: ed3ca5738ac9525c405a1fba954b49c78208afb9 [file] [log] [blame]
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001//===-- Instructions.cpp - Implement the LLVM instructions ----------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00008//===----------------------------------------------------------------------===//
9//
Chris Lattnerafdb3de2005-01-29 00:35:16 +000010// This file implements all of the non-inline methods for the LLVM instruction
11// classes.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000012//
13//===----------------------------------------------------------------------===//
14
Chandler Carruth9fb823b2013-01-02 11:36:10 +000015#include "llvm/IR/Instructions.h"
Devang Pateladd58652009-09-23 18:32:25 +000016#include "LLVMContextImpl.h"
Chandler Carruth219b89b2014-03-04 11:01:28 +000017#include "llvm/IR/CallSite.h"
Chandler Carruth8cd041e2014-03-04 12:24:34 +000018#include "llvm/IR/ConstantRange.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000019#include "llvm/IR/Constants.h"
20#include "llvm/IR/DataLayout.h"
21#include "llvm/IR/DerivedTypes.h"
22#include "llvm/IR/Function.h"
23#include "llvm/IR/Module.h"
24#include "llvm/IR/Operator.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000025#include "llvm/Support/ErrorHandling.h"
Christopher Lamb84485702007-04-22 19:24:39 +000026#include "llvm/Support/MathExtras.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000027using namespace llvm;
28
Chris Lattner3e13b8c2008-01-02 23:42:30 +000029//===----------------------------------------------------------------------===//
30// CallSite Class
31//===----------------------------------------------------------------------===//
32
Gabor Greifa2fbc0a2010-03-24 13:21:49 +000033User::op_iterator CallSite::getCallee() const {
34 Instruction *II(getInstruction());
35 return isCall()
Gabor Greif638c8232010-08-05 21:25:49 +000036 ? cast<CallInst>(II)->op_end() - 1 // Skip Callee
Gabor Greif6d673952010-07-16 09:38:02 +000037 : cast<InvokeInst>(II)->op_end() - 3; // Skip BB, BB, Callee
Gabor Greifa2fbc0a2010-03-24 13:21:49 +000038}
39
Gordon Henriksen14a55692007-12-10 02:14:30 +000040//===----------------------------------------------------------------------===//
41// TerminatorInst Class
42//===----------------------------------------------------------------------===//
43
44// Out of line virtual method, so the vtable, etc has a home.
45TerminatorInst::~TerminatorInst() {
46}
47
Reid Kleckner45a13e12017-05-11 21:26:55 +000048unsigned TerminatorInst::getNumSuccessors() const {
49 switch (getOpcode()) {
50#define HANDLE_TERM_INST(N, OPC, CLASS) \
51 case Instruction::OPC: \
52 return static_cast<const CLASS *>(this)->getNumSuccessorsV();
53#include "llvm/IR/Instruction.def"
54 default:
55 break;
56 }
57 llvm_unreachable("not a terminator");
58}
59
60BasicBlock *TerminatorInst::getSuccessor(unsigned idx) const {
61 switch (getOpcode()) {
62#define HANDLE_TERM_INST(N, OPC, CLASS) \
63 case Instruction::OPC: \
64 return static_cast<const CLASS *>(this)->getSuccessorV(idx);
65#include "llvm/IR/Instruction.def"
66 default:
67 break;
68 }
69 llvm_unreachable("not a terminator");
70}
71
72void TerminatorInst::setSuccessor(unsigned idx, BasicBlock *B) {
73 switch (getOpcode()) {
74#define HANDLE_TERM_INST(N, OPC, CLASS) \
75 case Instruction::OPC: \
76 return static_cast<CLASS *>(this)->setSuccessorV(idx, B);
77#include "llvm/IR/Instruction.def"
78 default:
79 break;
80 }
81 llvm_unreachable("not a terminator");
82}
83
Gabor Greiff6caff662008-05-10 08:32:32 +000084//===----------------------------------------------------------------------===//
85// UnaryInstruction Class
86//===----------------------------------------------------------------------===//
87
Gordon Henriksen14a55692007-12-10 02:14:30 +000088// Out of line virtual method, so the vtable, etc has a home.
89UnaryInstruction::~UnaryInstruction() {
90}
91
Chris Lattnerafdb3de2005-01-29 00:35:16 +000092//===----------------------------------------------------------------------===//
Chris Lattner88107952008-12-29 00:12:50 +000093// SelectInst Class
94//===----------------------------------------------------------------------===//
95
96/// areInvalidOperands - Return a string if the specified operands are invalid
97/// for a select operation, otherwise return null.
98const char *SelectInst::areInvalidOperands(Value *Op0, Value *Op1, Value *Op2) {
99 if (Op1->getType() != Op2->getType())
100 return "both values to select must have same type";
David Majnemerb611e3f2015-08-14 05:09:07 +0000101
102 if (Op1->getType()->isTokenTy())
103 return "select values cannot have token type";
104
Chris Lattner229907c2011-07-18 04:54:35 +0000105 if (VectorType *VT = dyn_cast<VectorType>(Op0->getType())) {
Chris Lattner88107952008-12-29 00:12:50 +0000106 // Vector select.
Owen Anderson55f1c092009-08-13 21:58:54 +0000107 if (VT->getElementType() != Type::getInt1Ty(Op0->getContext()))
Chris Lattner88107952008-12-29 00:12:50 +0000108 return "vector select condition element type must be i1";
Chris Lattner229907c2011-07-18 04:54:35 +0000109 VectorType *ET = dyn_cast<VectorType>(Op1->getType());
Craig Topperc6207612014-04-09 06:08:46 +0000110 if (!ET)
Chris Lattner88107952008-12-29 00:12:50 +0000111 return "selected values for vector select must be vectors";
112 if (ET->getNumElements() != VT->getNumElements())
113 return "vector select requires selected vectors to have "
114 "the same vector length as select condition";
Owen Anderson55f1c092009-08-13 21:58:54 +0000115 } else if (Op0->getType() != Type::getInt1Ty(Op0->getContext())) {
Chris Lattner88107952008-12-29 00:12:50 +0000116 return "select condition must be i1 or <n x i1>";
117 }
Craig Topperc6207612014-04-09 06:08:46 +0000118 return nullptr;
Chris Lattner88107952008-12-29 00:12:50 +0000119}
120
121
122//===----------------------------------------------------------------------===//
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000123// PHINode Class
124//===----------------------------------------------------------------------===//
125
Pete Cooper1d078692015-12-14 20:29:16 +0000126void PHINode::anchor() {}
127
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000128PHINode::PHINode(const PHINode &PN)
Pete Cooper3fc30402015-06-10 22:38:46 +0000129 : Instruction(PN.getType(), Instruction::PHI, nullptr, PN.getNumOperands()),
130 ReservedSpace(PN.getNumOperands()) {
131 allocHungoffUses(PN.getNumOperands());
Jay Foad61ea0e42011-06-23 09:09:15 +0000132 std::copy(PN.op_begin(), PN.op_end(), op_begin());
133 std::copy(PN.block_begin(), PN.block_end(), block_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000134 SubclassOptionalData = PN.SubclassOptionalData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000135}
136
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000137// removeIncomingValue - Remove an incoming value. This is useful if a
138// predecessor basic block is deleted.
139Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
Jay Foad61ea0e42011-06-23 09:09:15 +0000140 Value *Removed = getIncomingValue(Idx);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000141
142 // Move everything after this operand down.
143 //
144 // FIXME: we could just swap with the end of the list, then erase. However,
Jay Foad61ea0e42011-06-23 09:09:15 +0000145 // clients might not expect this to happen. The code as it is thrashes the
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000146 // use/def lists, which is kinda lame.
Jay Foad61ea0e42011-06-23 09:09:15 +0000147 std::copy(op_begin() + Idx + 1, op_end(), op_begin() + Idx);
148 std::copy(block_begin() + Idx + 1, block_end(), block_begin() + Idx);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000149
150 // Nuke the last value.
Craig Topperc6207612014-04-09 06:08:46 +0000151 Op<-1>().set(nullptr);
Pete Cooperb4eede22015-06-12 17:48:10 +0000152 setNumHungOffUseOperands(getNumOperands() - 1);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000153
154 // If the PHI node is dead, because it has zero entries, nuke it now.
Jay Foad61ea0e42011-06-23 09:09:15 +0000155 if (getNumOperands() == 0 && DeletePHIIfEmpty) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000156 // If anyone is using this PHI, make them use a dummy value instead...
Owen Andersonb292b8c2009-07-30 23:03:37 +0000157 replaceAllUsesWith(UndefValue::get(getType()));
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000158 eraseFromParent();
159 }
160 return Removed;
161}
162
Jay Foade98f29d2011-04-01 08:00:58 +0000163/// growOperands - grow operands - This grows the operand list in response
164/// to a push_back style of operation. This grows the number of ops by 1.5
165/// times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000166///
Jay Foade98f29d2011-04-01 08:00:58 +0000167void PHINode::growOperands() {
Gabor Greiff6caff662008-05-10 08:32:32 +0000168 unsigned e = getNumOperands();
Jay Foad61ea0e42011-06-23 09:09:15 +0000169 unsigned NumOps = e + e / 2;
170 if (NumOps < 2) NumOps = 2; // 2 op PHI nodes are VERY common.
171
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000172 ReservedSpace = NumOps;
Pete Cooper93f9ff52015-06-10 22:38:41 +0000173 growHungoffUses(ReservedSpace, /* IsPhi */ true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000174}
175
Nate Begemanb3923212005-08-04 23:24:19 +0000176/// hasConstantValue - If the specified PHI node always merges together the same
177/// value, return the value, otherwise return null.
Duncan Sands7412f6e2010-11-17 04:30:22 +0000178Value *PHINode::hasConstantValue() const {
179 // Exploit the fact that phi nodes always have at least one entry.
180 Value *ConstantValue = getIncomingValue(0);
181 for (unsigned i = 1, e = getNumIncomingValues(); i != e; ++i)
Nuno Lopes90c76df2012-07-03 17:10:28 +0000182 if (getIncomingValue(i) != ConstantValue && getIncomingValue(i) != this) {
183 if (ConstantValue != this)
Craig Topperc6207612014-04-09 06:08:46 +0000184 return nullptr; // Incoming values not all the same.
Nuno Lopes90c76df2012-07-03 17:10:28 +0000185 // The case where the first value is this PHI.
186 ConstantValue = getIncomingValue(i);
187 }
Nuno Lopes0d44a502012-07-03 21:15:40 +0000188 if (ConstantValue == this)
189 return UndefValue::get(getType());
Duncan Sands7412f6e2010-11-17 04:30:22 +0000190 return ConstantValue;
Nate Begemanb3923212005-08-04 23:24:19 +0000191}
192
Nicolai Haehnle13d90f32016-04-14 17:42:47 +0000193/// hasConstantOrUndefValue - Whether the specified PHI node always merges
194/// together the same value, assuming that undefs result in the same value as
195/// non-undefs.
196/// Unlike \ref hasConstantValue, this does not return a value because the
197/// unique non-undef incoming value need not dominate the PHI node.
198bool PHINode::hasConstantOrUndefValue() const {
199 Value *ConstantValue = nullptr;
200 for (unsigned i = 0, e = getNumIncomingValues(); i != e; ++i) {
201 Value *Incoming = getIncomingValue(i);
202 if (Incoming != this && !isa<UndefValue>(Incoming)) {
203 if (ConstantValue && ConstantValue != Incoming)
204 return false;
205 ConstantValue = Incoming;
206 }
207 }
208 return true;
209}
210
Bill Wendlingfae14752011-08-12 20:24:12 +0000211//===----------------------------------------------------------------------===//
212// LandingPadInst Implementation
213//===----------------------------------------------------------------------===//
214
David Majnemer7fddecc2015-06-17 20:52:32 +0000215LandingPadInst::LandingPadInst(Type *RetTy, unsigned NumReservedValues,
216 const Twine &NameStr, Instruction *InsertBefore)
217 : Instruction(RetTy, Instruction::LandingPad, nullptr, 0, InsertBefore) {
218 init(NumReservedValues, NameStr);
Bill Wendlingfae14752011-08-12 20:24:12 +0000219}
220
David Majnemer7fddecc2015-06-17 20:52:32 +0000221LandingPadInst::LandingPadInst(Type *RetTy, unsigned NumReservedValues,
222 const Twine &NameStr, BasicBlock *InsertAtEnd)
223 : Instruction(RetTy, Instruction::LandingPad, nullptr, 0, InsertAtEnd) {
224 init(NumReservedValues, NameStr);
Bill Wendlingfae14752011-08-12 20:24:12 +0000225}
226
227LandingPadInst::LandingPadInst(const LandingPadInst &LP)
Pete Cooper3fc30402015-06-10 22:38:46 +0000228 : Instruction(LP.getType(), Instruction::LandingPad, nullptr,
229 LP.getNumOperands()),
230 ReservedSpace(LP.getNumOperands()) {
231 allocHungoffUses(LP.getNumOperands());
Pete Cooper74510a42015-06-12 17:48:05 +0000232 Use *OL = getOperandList();
233 const Use *InOL = LP.getOperandList();
Bill Wendlingfae14752011-08-12 20:24:12 +0000234 for (unsigned I = 0, E = ReservedSpace; I != E; ++I)
235 OL[I] = InOL[I];
236
237 setCleanup(LP.isCleanup());
238}
239
David Majnemer7fddecc2015-06-17 20:52:32 +0000240LandingPadInst *LandingPadInst::Create(Type *RetTy, unsigned NumReservedClauses,
Bill Wendlingfae14752011-08-12 20:24:12 +0000241 const Twine &NameStr,
242 Instruction *InsertBefore) {
David Majnemer7fddecc2015-06-17 20:52:32 +0000243 return new LandingPadInst(RetTy, NumReservedClauses, NameStr, InsertBefore);
Bill Wendlingfae14752011-08-12 20:24:12 +0000244}
245
David Majnemer7fddecc2015-06-17 20:52:32 +0000246LandingPadInst *LandingPadInst::Create(Type *RetTy, unsigned NumReservedClauses,
Bill Wendlingfae14752011-08-12 20:24:12 +0000247 const Twine &NameStr,
248 BasicBlock *InsertAtEnd) {
David Majnemer7fddecc2015-06-17 20:52:32 +0000249 return new LandingPadInst(RetTy, NumReservedClauses, NameStr, InsertAtEnd);
Bill Wendlingfae14752011-08-12 20:24:12 +0000250}
251
David Majnemer7fddecc2015-06-17 20:52:32 +0000252void LandingPadInst::init(unsigned NumReservedValues, const Twine &NameStr) {
Bill Wendlingfae14752011-08-12 20:24:12 +0000253 ReservedSpace = NumReservedValues;
David Majnemer7fddecc2015-06-17 20:52:32 +0000254 setNumHungOffUseOperands(0);
Pete Cooper3fc30402015-06-10 22:38:46 +0000255 allocHungoffUses(ReservedSpace);
Bill Wendlingfae14752011-08-12 20:24:12 +0000256 setName(NameStr);
257 setCleanup(false);
258}
259
260/// growOperands - grow operands - This grows the operand list in response to a
261/// push_back style of operation. This grows the number of ops by 2 times.
262void LandingPadInst::growOperands(unsigned Size) {
263 unsigned e = getNumOperands();
264 if (ReservedSpace >= e + Size) return;
David Majnemer7fddecc2015-06-17 20:52:32 +0000265 ReservedSpace = (std::max(e, 1U) + Size / 2) * 2;
Pete Cooper93f9ff52015-06-10 22:38:41 +0000266 growHungoffUses(ReservedSpace);
Bill Wendlingfae14752011-08-12 20:24:12 +0000267}
268
Rafael Espindola4dc5dfc2014-06-04 18:51:31 +0000269void LandingPadInst::addClause(Constant *Val) {
Bill Wendlingfae14752011-08-12 20:24:12 +0000270 unsigned OpNo = getNumOperands();
271 growOperands(1);
272 assert(OpNo < ReservedSpace && "Growing didn't work!");
Pete Cooperb4eede22015-06-12 17:48:10 +0000273 setNumHungOffUseOperands(getNumOperands() + 1);
Pete Cooper74510a42015-06-12 17:48:05 +0000274 getOperandList()[OpNo] = Val;
Bill Wendlingfae14752011-08-12 20:24:12 +0000275}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000276
277//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000278// CallInst Implementation
279//===----------------------------------------------------------------------===//
280
Gordon Henriksen14a55692007-12-10 02:14:30 +0000281CallInst::~CallInst() {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000282}
283
David Blaikie348de692015-04-23 21:36:23 +0000284void CallInst::init(FunctionType *FTy, Value *Func, ArrayRef<Value *> Args,
Sanjoy Das9303c242015-09-24 19:14:18 +0000285 ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr) {
David Blaikie348de692015-04-23 21:36:23 +0000286 this->FTy = FTy;
Sanjoy Das9303c242015-09-24 19:14:18 +0000287 assert(getNumOperands() == Args.size() + CountBundleInputs(Bundles) + 1 &&
288 "NumOperands not set up?");
Gabor Greif6d673952010-07-16 09:38:02 +0000289 Op<-1>() = Func;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000290
Jay Foad5bd375a2011-07-15 08:37:34 +0000291#ifndef NDEBUG
Jay Foad5bd375a2011-07-15 08:37:34 +0000292 assert((Args.size() == FTy->getNumParams() ||
293 (FTy->isVarArg() && Args.size() > FTy->getNumParams())) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000294 "Calling a function with bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000295
296 for (unsigned i = 0; i != Args.size(); ++i)
Chris Lattner667a0562006-05-03 00:48:22 +0000297 assert((i >= FTy->getNumParams() ||
Jay Foad5bd375a2011-07-15 08:37:34 +0000298 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000299 "Calling a function with a bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000300#endif
301
302 std::copy(Args.begin(), Args.end(), op_begin());
Sanjoy Das9303c242015-09-24 19:14:18 +0000303
304 auto It = populateBundleOperandInfos(Bundles, Args.size());
305 (void)It;
306 assert(It + 1 == op_end() && "Should add up!");
307
Jay Foad5bd375a2011-07-15 08:37:34 +0000308 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000309}
310
Jay Foad5bd375a2011-07-15 08:37:34 +0000311void CallInst::init(Value *Func, const Twine &NameStr) {
David Blaikie348de692015-04-23 21:36:23 +0000312 FTy =
313 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Pete Cooperb4eede22015-06-12 17:48:10 +0000314 assert(getNumOperands() == 1 && "NumOperands not set up?");
Gabor Greif6d673952010-07-16 09:38:02 +0000315 Op<-1>() = Func;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000316
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000317 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
Jay Foad5bd375a2011-07-15 08:37:34 +0000318
319 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000320}
321
Daniel Dunbar4975db62009-07-25 04:41:11 +0000322CallInst::CallInst(Value *Func, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000323 Instruction *InsertBefore)
324 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
325 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000326 Instruction::Call,
327 OperandTraits<CallInst>::op_end(this) - 1,
328 1, InsertBefore) {
Jay Foad5bd375a2011-07-15 08:37:34 +0000329 init(Func, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000330}
331
Daniel Dunbar4975db62009-07-25 04:41:11 +0000332CallInst::CallInst(Value *Func, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000333 BasicBlock *InsertAtEnd)
334 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
335 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000336 Instruction::Call,
337 OperandTraits<CallInst>::op_end(this) - 1,
338 1, InsertAtEnd) {
Jay Foad5bd375a2011-07-15 08:37:34 +0000339 init(Func, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000340}
341
Misha Brukmanb1c93172005-04-21 23:48:37 +0000342CallInst::CallInst(const CallInst &CI)
David Blaikie348de692015-04-23 21:36:23 +0000343 : Instruction(CI.getType(), Instruction::Call,
344 OperandTraits<CallInst>::op_end(this) - CI.getNumOperands(),
345 CI.getNumOperands()),
Reid Klecknerb5180542017-03-21 16:57:19 +0000346 Attrs(CI.Attrs), FTy(CI.FTy) {
Reid Kleckner118e1bf2014-05-06 20:08:20 +0000347 setTailCallKind(CI.getTailCallKind());
Chris Lattnerb9c86512009-12-29 02:14:09 +0000348 setCallingConv(CI.getCallingConv());
Sanjoy Das9303c242015-09-24 19:14:18 +0000349
Jay Foad5bd375a2011-07-15 08:37:34 +0000350 std::copy(CI.op_begin(), CI.op_end(), op_begin());
Sanjoy Das9303c242015-09-24 19:14:18 +0000351 std::copy(CI.bundle_op_info_begin(), CI.bundle_op_info_end(),
352 bundle_op_info_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000353 SubclassOptionalData = CI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000354}
355
Sanjoy Das2d161452015-11-18 06:23:38 +0000356CallInst *CallInst::Create(CallInst *CI, ArrayRef<OperandBundleDef> OpB,
357 Instruction *InsertPt) {
Sanjoy Dasccd14562015-12-10 06:39:02 +0000358 std::vector<Value *> Args(CI->arg_begin(), CI->arg_end());
Sanjoy Das2d161452015-11-18 06:23:38 +0000359
360 auto *NewCI = CallInst::Create(CI->getCalledValue(), Args, OpB, CI->getName(),
361 InsertPt);
362 NewCI->setTailCallKind(CI->getTailCallKind());
363 NewCI->setCallingConv(CI->getCallingConv());
364 NewCI->SubclassOptionalData = CI->SubclassOptionalData;
Sanjoy Dasb8dced52015-12-09 01:01:28 +0000365 NewCI->setAttributes(CI->getAttributes());
Joseph Tremouletbba70e42016-01-14 06:21:42 +0000366 NewCI->setDebugLoc(CI->getDebugLoc());
Sanjoy Das2d161452015-11-18 06:23:38 +0000367 return NewCI;
368}
369
Hal Finkele87ad542016-07-10 23:01:32 +0000370Value *CallInst::getReturnedArgOperand() const {
371 unsigned Index;
372
Reid Klecknerb5180542017-03-21 16:57:19 +0000373 if (Attrs.hasAttrSomewhere(Attribute::Returned, &Index) && Index)
Reid Klecknera0b45f42017-05-03 18:17:31 +0000374 return getArgOperand(Index - AttributeList::FirstArgIndex);
Hal Finkele87ad542016-07-10 23:01:32 +0000375 if (const Function *F = getCalledFunction())
376 if (F->getAttributes().hasAttrSomewhere(Attribute::Returned, &Index) &&
377 Index)
Reid Klecknera0b45f42017-05-03 18:17:31 +0000378 return getArgOperand(Index - AttributeList::FirstArgIndex);
379
Hal Finkele87ad542016-07-10 23:01:32 +0000380 return nullptr;
381}
382
Amaury Sechet392638d2016-06-14 20:27:35 +0000383void CallInst::addAttribute(unsigned i, Attribute::AttrKind Kind) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000384 AttributeList PAL = getAttributes();
Amaury Sechet392638d2016-06-14 20:27:35 +0000385 PAL = PAL.addAttribute(getContext(), i, Kind);
Devang Patel4c758ea2008-09-25 21:00:45 +0000386 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000387}
388
Amaury Secheta65a2372016-06-15 05:14:29 +0000389void CallInst::addAttribute(unsigned i, Attribute Attr) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000390 AttributeList PAL = getAttributes();
Amaury Secheta65a2372016-06-15 05:14:29 +0000391 PAL = PAL.addAttribute(getContext(), i, Attr);
392 setAttributes(PAL);
393}
394
Reid Klecknera0b45f42017-05-03 18:17:31 +0000395void CallInst::addParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) {
396 addAttribute(ArgNo + AttributeList::FirstArgIndex, Kind);
397}
398
Amaury Sechet392638d2016-06-14 20:27:35 +0000399void CallInst::removeAttribute(unsigned i, Attribute::AttrKind Kind) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000400 AttributeList PAL = getAttributes();
Amaury Sechet392638d2016-06-14 20:27:35 +0000401 PAL = PAL.removeAttribute(getContext(), i, Kind);
Amaury Sechet1a0e0972016-04-21 21:29:10 +0000402 setAttributes(PAL);
403}
404
Amaury Sechet6100adf2016-06-15 17:50:39 +0000405void CallInst::removeAttribute(unsigned i, StringRef Kind) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000406 AttributeList PAL = getAttributes();
Amaury Sechet6100adf2016-06-15 17:50:39 +0000407 PAL = PAL.removeAttribute(getContext(), i, Kind);
408 setAttributes(PAL);
409}
410
Reid Klecknera0b45f42017-05-03 18:17:31 +0000411void CallInst::removeParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) {
412 removeAttribute(ArgNo + AttributeList::FirstArgIndex, Kind);
413}
414
Ramkumar Ramachandra8fcb4982015-02-14 19:37:54 +0000415void CallInst::addDereferenceableAttr(unsigned i, uint64_t Bytes) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000416 AttributeList PAL = getAttributes();
Ramkumar Ramachandra8fcb4982015-02-14 19:37:54 +0000417 PAL = PAL.addDereferenceableAttr(getContext(), i, Bytes);
418 setAttributes(PAL);
419}
420
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000421void CallInst::addDereferenceableOrNullAttr(unsigned i, uint64_t Bytes) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000422 AttributeList PAL = getAttributes();
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000423 PAL = PAL.addDereferenceableOrNullAttr(getContext(), i, Bytes);
424 setAttributes(PAL);
425}
426
Reid Klecknerfb502d22017-04-14 20:19:02 +0000427bool CallInst::hasRetAttr(Attribute::AttrKind Kind) const {
428 if (Attrs.hasAttribute(AttributeList::ReturnIndex, Kind))
429 return true;
Sanjoy Dasb11b4402015-11-04 20:33:45 +0000430
Reid Klecknerfb502d22017-04-14 20:19:02 +0000431 // Look at the callee, if available.
432 if (const Function *F = getCalledFunction())
433 return F->getAttributes().hasAttribute(AttributeList::ReturnIndex, Kind);
434 return false;
435}
436
437bool CallInst::paramHasAttr(unsigned i, Attribute::AttrKind Kind) const {
438 assert(i < getNumArgOperands() && "Param index out of bounds!");
439
440 if (Attrs.hasParamAttribute(i, Kind))
Bill Wendling8baa61d2012-10-03 17:54:26 +0000441 return true;
442 if (const Function *F = getCalledFunction())
Reid Klecknerfb502d22017-04-14 20:19:02 +0000443 return F->getAttributes().hasParamAttribute(i, Kind);
Bill Wendlingdaf8e382012-10-04 07:18:12 +0000444 return false;
445}
446
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000447bool CallInst::dataOperandHasImpliedAttr(unsigned i,
Amaury Sechet392638d2016-06-14 20:27:35 +0000448 Attribute::AttrKind Kind) const {
Sanjoy Das776e4a72015-11-05 01:53:26 +0000449 // There are getNumOperands() - 1 data operands. The last operand is the
450 // callee.
451 assert(i < getNumOperands() && "Data operand index out of bounds!");
452
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000453 // The attribute A can either be directly specified, if the operand in
454 // question is a call argument; or be indirectly implied by the kind of its
455 // containing operand bundle, if the operand is a bundle operand.
456
Reid Klecknerfb502d22017-04-14 20:19:02 +0000457 // FIXME: Avoid these i - 1 calculations and update the API to use zero-based
458 // indices.
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000459 if (i < (getNumArgOperands() + 1))
Reid Klecknerfb502d22017-04-14 20:19:02 +0000460 return paramHasAttr(i - 1, Kind);
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000461
462 assert(hasOperandBundles() && i >= (getBundleOperandsStartIndex() + 1) &&
463 "Must be either a call argument or an operand bundle!");
Amaury Sechet392638d2016-06-14 20:27:35 +0000464 return bundleOperandHasAttr(i - 1, Kind);
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000465}
466
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000467/// IsConstantOne - Return true only if val is constant int 1
468static bool IsConstantOne(Value *val) {
Reid Kleckner971c3ea2014-11-13 22:55:19 +0000469 assert(val && "IsConstantOne does not work with nullptr val");
Matt Arsenault69417852014-09-15 17:56:51 +0000470 const ConstantInt *CVal = dyn_cast<ConstantInt>(val);
471 return CVal && CVal->isOne();
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000472}
473
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000474static Instruction *createMalloc(Instruction *InsertBefore,
Chris Lattner229907c2011-07-18 04:54:35 +0000475 BasicBlock *InsertAtEnd, Type *IntPtrTy,
David Majnemerfadc6db2016-04-29 08:07:22 +0000476 Type *AllocTy, Value *AllocSize,
477 Value *ArraySize,
478 ArrayRef<OperandBundleDef> OpB,
479 Function *MallocF, const Twine &Name) {
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000480 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
Victor Hernandez788eaab2009-09-18 19:20:02 +0000481 "createMalloc needs either InsertBefore or InsertAtEnd");
482
483 // malloc(type) becomes:
484 // bitcast (i8* malloc(typeSize)) to type*
485 // malloc(type, arraySize) becomes:
Ana Pazosb3596022016-02-03 21:34:39 +0000486 // bitcast (i8* malloc(typeSize*arraySize)) to type*
Victor Hernandezf3db9152009-11-07 00:16:28 +0000487 if (!ArraySize)
488 ArraySize = ConstantInt::get(IntPtrTy, 1);
489 else if (ArraySize->getType() != IntPtrTy) {
490 if (InsertBefore)
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000491 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
492 "", InsertBefore);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000493 else
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000494 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
495 "", InsertAtEnd);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000496 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000497
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000498 if (!IsConstantOne(ArraySize)) {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000499 if (IsConstantOne(AllocSize)) {
500 AllocSize = ArraySize; // Operand * 1 = Operand
501 } else if (Constant *CO = dyn_cast<Constant>(ArraySize)) {
502 Constant *Scale = ConstantExpr::getIntegerCast(CO, IntPtrTy,
503 false /*ZExt*/);
504 // Malloc arg is constant product of type size and array size
505 AllocSize = ConstantExpr::getMul(Scale, cast<Constant>(AllocSize));
506 } else {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000507 // Multiply type size by the array size...
508 if (InsertBefore)
Victor Hernandez788eaab2009-09-18 19:20:02 +0000509 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
510 "mallocsize", InsertBefore);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000511 else
Victor Hernandez788eaab2009-09-18 19:20:02 +0000512 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
513 "mallocsize", InsertAtEnd);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000514 }
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000515 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000516
Victor Hernandez788eaab2009-09-18 19:20:02 +0000517 assert(AllocSize->getType() == IntPtrTy && "malloc arg is wrong size");
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000518 // Create the call to Malloc.
Ana Pazosb3596022016-02-03 21:34:39 +0000519 BasicBlock *BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
520 Module *M = BB->getParent()->getParent();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000521 Type *BPTy = Type::getInt8PtrTy(BB->getContext());
Victor Hernandezbb336a12009-11-10 19:53:28 +0000522 Value *MallocFunc = MallocF;
523 if (!MallocFunc)
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000524 // prototype malloc as "void *malloc(size_t)"
Serge Guelton59a2d7b2017-04-11 15:01:18 +0000525 MallocFunc = M->getOrInsertFunction("malloc", BPTy, IntPtrTy);
Chris Lattner229907c2011-07-18 04:54:35 +0000526 PointerType *AllocPtrType = PointerType::getUnqual(AllocTy);
Craig Topperc6207612014-04-09 06:08:46 +0000527 CallInst *MCall = nullptr;
528 Instruction *Result = nullptr;
Victor Hernandez788eaab2009-09-18 19:20:02 +0000529 if (InsertBefore) {
David Majnemerfadc6db2016-04-29 08:07:22 +0000530 MCall = CallInst::Create(MallocFunc, AllocSize, OpB, "malloccall",
531 InsertBefore);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000532 Result = MCall;
533 if (Result->getType() != AllocPtrType)
534 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000535 Result = new BitCastInst(MCall, AllocPtrType, Name, InsertBefore);
Victor Hernandez788eaab2009-09-18 19:20:02 +0000536 } else {
David Majnemerfadc6db2016-04-29 08:07:22 +0000537 MCall = CallInst::Create(MallocFunc, AllocSize, OpB, "malloccall");
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000538 Result = MCall;
539 if (Result->getType() != AllocPtrType) {
540 InsertAtEnd->getInstList().push_back(MCall);
541 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000542 Result = new BitCastInst(MCall, AllocPtrType, Name);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000543 }
Victor Hernandez788eaab2009-09-18 19:20:02 +0000544 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000545 MCall->setTailCall();
Victor Hernandezbb336a12009-11-10 19:53:28 +0000546 if (Function *F = dyn_cast<Function>(MallocFunc)) {
547 MCall->setCallingConv(F->getCallingConv());
Reid Klecknera0b45f42017-05-03 18:17:31 +0000548 if (!F->returnDoesNotAlias())
549 F->setReturnDoesNotAlias();
Victor Hernandezbb336a12009-11-10 19:53:28 +0000550 }
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000551 assert(!MCall->getType()->isVoidTy() && "Malloc has void return type");
Victor Hernandez788eaab2009-09-18 19:20:02 +0000552
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000553 return Result;
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000554}
555
556/// CreateMalloc - Generate the IR for a call to malloc:
557/// 1. Compute the malloc call's argument as the specified type's size,
558/// possibly multiplied by the array size if the array size is not
559/// constant 1.
560/// 2. Call malloc with that argument.
561/// 3. Bitcast the result of the malloc call to the specified type.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000562Instruction *CallInst::CreateMalloc(Instruction *InsertBefore,
Chris Lattner229907c2011-07-18 04:54:35 +0000563 Type *IntPtrTy, Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000564 Value *AllocSize, Value *ArraySize,
Ana Pazosb3596022016-02-03 21:34:39 +0000565 Function *MallocF,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000566 const Twine &Name) {
Craig Topperc6207612014-04-09 06:08:46 +0000567 return createMalloc(InsertBefore, nullptr, IntPtrTy, AllocTy, AllocSize,
David Majnemerfadc6db2016-04-29 08:07:22 +0000568 ArraySize, None, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000569}
David Majnemerfadc6db2016-04-29 08:07:22 +0000570Instruction *CallInst::CreateMalloc(Instruction *InsertBefore,
571 Type *IntPtrTy, Type *AllocTy,
572 Value *AllocSize, Value *ArraySize,
573 ArrayRef<OperandBundleDef> OpB,
574 Function *MallocF,
575 const Twine &Name) {
576 return createMalloc(InsertBefore, nullptr, IntPtrTy, AllocTy, AllocSize,
577 ArraySize, OpB, MallocF, Name);
578}
579
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000580
581/// CreateMalloc - Generate the IR for a call to malloc:
582/// 1. Compute the malloc call's argument as the specified type's size,
583/// possibly multiplied by the array size if the array size is not
584/// constant 1.
585/// 2. Call malloc with that argument.
586/// 3. Bitcast the result of the malloc call to the specified type.
587/// Note: This function does not add the bitcast to the basic block, that is the
588/// responsibility of the caller.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000589Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd,
Chris Lattner229907c2011-07-18 04:54:35 +0000590 Type *IntPtrTy, Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000591 Value *AllocSize, Value *ArraySize,
592 Function *MallocF, const Twine &Name) {
Craig Topperc6207612014-04-09 06:08:46 +0000593 return createMalloc(nullptr, InsertAtEnd, IntPtrTy, AllocTy, AllocSize,
David Majnemerfadc6db2016-04-29 08:07:22 +0000594 ArraySize, None, MallocF, Name);
595}
596Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd,
597 Type *IntPtrTy, Type *AllocTy,
598 Value *AllocSize, Value *ArraySize,
599 ArrayRef<OperandBundleDef> OpB,
600 Function *MallocF, const Twine &Name) {
601 return createMalloc(nullptr, InsertAtEnd, IntPtrTy, AllocTy, AllocSize,
602 ArraySize, OpB, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000603}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000604
David Majnemerfadc6db2016-04-29 08:07:22 +0000605static Instruction *createFree(Value *Source,
606 ArrayRef<OperandBundleDef> Bundles,
607 Instruction *InsertBefore,
Victor Hernandeze2971492009-10-24 04:23:03 +0000608 BasicBlock *InsertAtEnd) {
609 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
610 "createFree needs either InsertBefore or InsertAtEnd");
Duncan Sands19d0b472010-02-16 11:11:14 +0000611 assert(Source->getType()->isPointerTy() &&
Victor Hernandeze2971492009-10-24 04:23:03 +0000612 "Can not free something of nonpointer type!");
613
Ana Pazosb3596022016-02-03 21:34:39 +0000614 BasicBlock *BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
615 Module *M = BB->getParent()->getParent();
Victor Hernandeze2971492009-10-24 04:23:03 +0000616
Chris Lattner229907c2011-07-18 04:54:35 +0000617 Type *VoidTy = Type::getVoidTy(M->getContext());
618 Type *IntPtrTy = Type::getInt8PtrTy(M->getContext());
Victor Hernandeze2971492009-10-24 04:23:03 +0000619 // prototype free as "void free(void*)"
Serge Guelton59a2d7b2017-04-11 15:01:18 +0000620 Value *FreeFunc = M->getOrInsertFunction("free", VoidTy, IntPtrTy);
Ana Pazosb3596022016-02-03 21:34:39 +0000621 CallInst *Result = nullptr;
Victor Hernandeze2971492009-10-24 04:23:03 +0000622 Value *PtrCast = Source;
623 if (InsertBefore) {
624 if (Source->getType() != IntPtrTy)
625 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertBefore);
David Majnemerfadc6db2016-04-29 08:07:22 +0000626 Result = CallInst::Create(FreeFunc, PtrCast, Bundles, "", InsertBefore);
Victor Hernandeze2971492009-10-24 04:23:03 +0000627 } else {
628 if (Source->getType() != IntPtrTy)
629 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertAtEnd);
David Majnemerfadc6db2016-04-29 08:07:22 +0000630 Result = CallInst::Create(FreeFunc, PtrCast, Bundles, "");
Victor Hernandeze2971492009-10-24 04:23:03 +0000631 }
632 Result->setTailCall();
Chris Lattner2156c222009-11-09 07:12:01 +0000633 if (Function *F = dyn_cast<Function>(FreeFunc))
634 Result->setCallingConv(F->getCallingConv());
Victor Hernandeze2971492009-10-24 04:23:03 +0000635
636 return Result;
637}
638
639/// CreateFree - Generate the IR for a call to the builtin free function.
Ana Pazosb3596022016-02-03 21:34:39 +0000640Instruction *CallInst::CreateFree(Value *Source, Instruction *InsertBefore) {
David Majnemerfadc6db2016-04-29 08:07:22 +0000641 return createFree(Source, None, InsertBefore, nullptr);
642}
643Instruction *CallInst::CreateFree(Value *Source,
644 ArrayRef<OperandBundleDef> Bundles,
645 Instruction *InsertBefore) {
646 return createFree(Source, Bundles, InsertBefore, nullptr);
Victor Hernandeze2971492009-10-24 04:23:03 +0000647}
648
649/// CreateFree - Generate the IR for a call to the builtin free function.
650/// Note: This function does not add the call to the basic block, that is the
651/// responsibility of the caller.
Ana Pazosb3596022016-02-03 21:34:39 +0000652Instruction *CallInst::CreateFree(Value *Source, BasicBlock *InsertAtEnd) {
David Majnemerfadc6db2016-04-29 08:07:22 +0000653 Instruction *FreeCall = createFree(Source, None, nullptr, InsertAtEnd);
654 assert(FreeCall && "CreateFree did not create a CallInst");
655 return FreeCall;
656}
657Instruction *CallInst::CreateFree(Value *Source,
658 ArrayRef<OperandBundleDef> Bundles,
659 BasicBlock *InsertAtEnd) {
660 Instruction *FreeCall = createFree(Source, Bundles, nullptr, InsertAtEnd);
Victor Hernandeze2971492009-10-24 04:23:03 +0000661 assert(FreeCall && "CreateFree did not create a CallInst");
662 return FreeCall;
663}
664
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000665//===----------------------------------------------------------------------===//
666// InvokeInst Implementation
667//===----------------------------------------------------------------------===//
668
David Blaikie3e807092015-05-13 18:35:26 +0000669void InvokeInst::init(FunctionType *FTy, Value *Fn, BasicBlock *IfNormal,
670 BasicBlock *IfException, ArrayRef<Value *> Args,
Sanjoy Das9303c242015-09-24 19:14:18 +0000671 ArrayRef<OperandBundleDef> Bundles,
David Blaikie3e807092015-05-13 18:35:26 +0000672 const Twine &NameStr) {
673 this->FTy = FTy;
David Blaikie348de692015-04-23 21:36:23 +0000674
Sanjoy Das9303c242015-09-24 19:14:18 +0000675 assert(getNumOperands() == 3 + Args.size() + CountBundleInputs(Bundles) &&
676 "NumOperands not set up?");
Gabor Greifa2fbc0a2010-03-24 13:21:49 +0000677 Op<-3>() = Fn;
678 Op<-2>() = IfNormal;
679 Op<-1>() = IfException;
Jay Foad5bd375a2011-07-15 08:37:34 +0000680
681#ifndef NDEBUG
Jay Foad5bd375a2011-07-15 08:37:34 +0000682 assert(((Args.size() == FTy->getNumParams()) ||
683 (FTy->isVarArg() && Args.size() > FTy->getNumParams())) &&
Gabor Greif668d7002010-03-23 13:45:54 +0000684 "Invoking a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000685
Jay Foad5bd375a2011-07-15 08:37:34 +0000686 for (unsigned i = 0, e = Args.size(); i != e; i++)
Chris Lattner667a0562006-05-03 00:48:22 +0000687 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000688 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000689 "Invoking a function with a bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000690#endif
691
692 std::copy(Args.begin(), Args.end(), op_begin());
Sanjoy Das9303c242015-09-24 19:14:18 +0000693
694 auto It = populateBundleOperandInfos(Bundles, Args.size());
695 (void)It;
696 assert(It + 3 == op_end() && "Should add up!");
697
Jay Foad5bd375a2011-07-15 08:37:34 +0000698 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000699}
700
Misha Brukmanb1c93172005-04-21 23:48:37 +0000701InvokeInst::InvokeInst(const InvokeInst &II)
David Blaikie348de692015-04-23 21:36:23 +0000702 : TerminatorInst(II.getType(), Instruction::Invoke,
703 OperandTraits<InvokeInst>::op_end(this) -
704 II.getNumOperands(),
705 II.getNumOperands()),
Reid Klecknerb5180542017-03-21 16:57:19 +0000706 Attrs(II.Attrs), FTy(II.FTy) {
Chris Lattnerb9c86512009-12-29 02:14:09 +0000707 setCallingConv(II.getCallingConv());
Jay Foad5bd375a2011-07-15 08:37:34 +0000708 std::copy(II.op_begin(), II.op_end(), op_begin());
Sanjoy Das9303c242015-09-24 19:14:18 +0000709 std::copy(II.bundle_op_info_begin(), II.bundle_op_info_end(),
710 bundle_op_info_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000711 SubclassOptionalData = II.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000712}
713
Sanjoy Das2d161452015-11-18 06:23:38 +0000714InvokeInst *InvokeInst::Create(InvokeInst *II, ArrayRef<OperandBundleDef> OpB,
715 Instruction *InsertPt) {
Sanjoy Dasccd14562015-12-10 06:39:02 +0000716 std::vector<Value *> Args(II->arg_begin(), II->arg_end());
Sanjoy Das2d161452015-11-18 06:23:38 +0000717
718 auto *NewII = InvokeInst::Create(II->getCalledValue(), II->getNormalDest(),
719 II->getUnwindDest(), Args, OpB,
720 II->getName(), InsertPt);
721 NewII->setCallingConv(II->getCallingConv());
722 NewII->SubclassOptionalData = II->SubclassOptionalData;
Sanjoy Dasb8dced52015-12-09 01:01:28 +0000723 NewII->setAttributes(II->getAttributes());
Joseph Tremouletbba70e42016-01-14 06:21:42 +0000724 NewII->setDebugLoc(II->getDebugLoc());
Sanjoy Das2d161452015-11-18 06:23:38 +0000725 return NewII;
726}
727
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000728BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
729 return getSuccessor(idx);
730}
731unsigned InvokeInst::getNumSuccessorsV() const {
732 return getNumSuccessors();
733}
734void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
735 return setSuccessor(idx, B);
736}
737
Hal Finkele87ad542016-07-10 23:01:32 +0000738Value *InvokeInst::getReturnedArgOperand() const {
739 unsigned Index;
740
Reid Klecknerb5180542017-03-21 16:57:19 +0000741 if (Attrs.hasAttrSomewhere(Attribute::Returned, &Index) && Index)
Reid Klecknera0b45f42017-05-03 18:17:31 +0000742 return getArgOperand(Index - AttributeList::FirstArgIndex);
Hal Finkele87ad542016-07-10 23:01:32 +0000743 if (const Function *F = getCalledFunction())
744 if (F->getAttributes().hasAttrSomewhere(Attribute::Returned, &Index) &&
745 Index)
Reid Klecknera0b45f42017-05-03 18:17:31 +0000746 return getArgOperand(Index - AttributeList::FirstArgIndex);
747
Hal Finkele87ad542016-07-10 23:01:32 +0000748 return nullptr;
749}
750
Reid Klecknerfb502d22017-04-14 20:19:02 +0000751bool InvokeInst::hasRetAttr(Attribute::AttrKind Kind) const {
752 if (Attrs.hasAttribute(AttributeList::ReturnIndex, Kind))
753 return true;
Sanjoy Dasb11b4402015-11-04 20:33:45 +0000754
Reid Klecknerfb502d22017-04-14 20:19:02 +0000755 // Look at the callee, if available.
756 if (const Function *F = getCalledFunction())
757 return F->getAttributes().hasAttribute(AttributeList::ReturnIndex, Kind);
758 return false;
759}
760
761bool InvokeInst::paramHasAttr(unsigned i, Attribute::AttrKind Kind) const {
762 assert(i < getNumArgOperands() && "Param index out of bounds!");
763
764 if (Attrs.hasParamAttribute(i, Kind))
Bill Wendling8baa61d2012-10-03 17:54:26 +0000765 return true;
766 if (const Function *F = getCalledFunction())
Reid Klecknerfb502d22017-04-14 20:19:02 +0000767 return F->getAttributes().hasParamAttribute(i, Kind);
Bill Wendlingdaf8e382012-10-04 07:18:12 +0000768 return false;
769}
770
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000771bool InvokeInst::dataOperandHasImpliedAttr(unsigned i,
Amaury Sechet392638d2016-06-14 20:27:35 +0000772 Attribute::AttrKind Kind) const {
Sanjoy Das776e4a72015-11-05 01:53:26 +0000773 // There are getNumOperands() - 3 data operands. The last three operands are
774 // the callee and the two successor basic blocks.
775 assert(i < (getNumOperands() - 2) && "Data operand index out of bounds!");
776
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000777 // The attribute A can either be directly specified, if the operand in
778 // question is an invoke argument; or be indirectly implied by the kind of its
779 // containing operand bundle, if the operand is a bundle operand.
780
Reid Klecknerfb502d22017-04-14 20:19:02 +0000781 // FIXME: Avoid these i - 1 calculations and update the API to use zero-based
782 // indices.
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000783 if (i < (getNumArgOperands() + 1))
Reid Klecknerfb502d22017-04-14 20:19:02 +0000784 return paramHasAttr(i - 1, Kind);
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000785
786 assert(hasOperandBundles() && i >= (getBundleOperandsStartIndex() + 1) &&
787 "Must be either an invoke argument or an operand bundle!");
Amaury Sechet392638d2016-06-14 20:27:35 +0000788 return bundleOperandHasAttr(i - 1, Kind);
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000789}
790
Amaury Sechet392638d2016-06-14 20:27:35 +0000791void InvokeInst::addAttribute(unsigned i, Attribute::AttrKind Kind) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000792 AttributeList PAL = getAttributes();
Amaury Sechet392638d2016-06-14 20:27:35 +0000793 PAL = PAL.addAttribute(getContext(), i, Kind);
Devang Patel4c758ea2008-09-25 21:00:45 +0000794 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000795}
796
Amaury Secheta65a2372016-06-15 05:14:29 +0000797void InvokeInst::addAttribute(unsigned i, Attribute Attr) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000798 AttributeList PAL = getAttributes();
Amaury Secheta65a2372016-06-15 05:14:29 +0000799 PAL = PAL.addAttribute(getContext(), i, Attr);
800 setAttributes(PAL);
801}
802
Reid Klecknera0b45f42017-05-03 18:17:31 +0000803void InvokeInst::addParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) {
804 addAttribute(ArgNo + AttributeList::FirstArgIndex, Kind);
805}
806
Amaury Sechet392638d2016-06-14 20:27:35 +0000807void InvokeInst::removeAttribute(unsigned i, Attribute::AttrKind Kind) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000808 AttributeList PAL = getAttributes();
Amaury Sechet392638d2016-06-14 20:27:35 +0000809 PAL = PAL.removeAttribute(getContext(), i, Kind);
Amaury Sechet1a0e0972016-04-21 21:29:10 +0000810 setAttributes(PAL);
811}
812
Amaury Sechet6100adf2016-06-15 17:50:39 +0000813void InvokeInst::removeAttribute(unsigned i, StringRef Kind) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000814 AttributeList PAL = getAttributes();
Amaury Sechet6100adf2016-06-15 17:50:39 +0000815 PAL = PAL.removeAttribute(getContext(), i, Kind);
816 setAttributes(PAL);
817}
818
Reid Klecknera0b45f42017-05-03 18:17:31 +0000819void InvokeInst::removeParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) {
820 removeAttribute(ArgNo + AttributeList::FirstArgIndex, Kind);
821}
822
Ramkumar Ramachandra8fcb4982015-02-14 19:37:54 +0000823void InvokeInst::addDereferenceableAttr(unsigned i, uint64_t Bytes) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000824 AttributeList PAL = getAttributes();
Ramkumar Ramachandra8fcb4982015-02-14 19:37:54 +0000825 PAL = PAL.addDereferenceableAttr(getContext(), i, Bytes);
826 setAttributes(PAL);
827}
828
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000829void InvokeInst::addDereferenceableOrNullAttr(unsigned i, uint64_t Bytes) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000830 AttributeList PAL = getAttributes();
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000831 PAL = PAL.addDereferenceableOrNullAttr(getContext(), i, Bytes);
832 setAttributes(PAL);
833}
834
Bill Wendlingfae14752011-08-12 20:24:12 +0000835LandingPadInst *InvokeInst::getLandingPadInst() const {
836 return cast<LandingPadInst>(getUnwindDest()->getFirstNonPHI());
837}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000838
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000839//===----------------------------------------------------------------------===//
840// ReturnInst Implementation
841//===----------------------------------------------------------------------===//
842
Chris Lattner2195fc42007-02-24 00:55:48 +0000843ReturnInst::ReturnInst(const ReturnInst &RI)
Owen Anderson55f1c092009-08-13 21:58:54 +0000844 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000845 OperandTraits<ReturnInst>::op_end(this) -
846 RI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000847 RI.getNumOperands()) {
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000848 if (RI.getNumOperands())
Gabor Greif2d3024d2008-05-26 21:33:52 +0000849 Op<0>() = RI.Op<0>();
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000850 SubclassOptionalData = RI.SubclassOptionalData;
Chris Lattner2195fc42007-02-24 00:55:48 +0000851}
852
Owen Anderson55f1c092009-08-13 21:58:54 +0000853ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, Instruction *InsertBefore)
854 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000855 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
856 InsertBefore) {
Devang Patelc38eb522008-02-26 18:49:29 +0000857 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000858 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000859}
Owen Anderson55f1c092009-08-13 21:58:54 +0000860ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd)
861 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000862 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
863 InsertAtEnd) {
Devang Patelc38eb522008-02-26 18:49:29 +0000864 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000865 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000866}
Owen Anderson55f1c092009-08-13 21:58:54 +0000867ReturnInst::ReturnInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
868 : TerminatorInst(Type::getVoidTy(Context), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000869 OperandTraits<ReturnInst>::op_end(this), 0, InsertAtEnd) {
Devang Patel59643e52008-02-23 00:35:18 +0000870}
871
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000872unsigned ReturnInst::getNumSuccessorsV() const {
873 return getNumSuccessors();
874}
875
Devang Patelae682fb2008-02-26 17:56:20 +0000876/// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
877/// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000878void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000879 llvm_unreachable("ReturnInst has no successors!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000880}
881
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000882BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000883 llvm_unreachable("ReturnInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000884}
885
Devang Patel59643e52008-02-23 00:35:18 +0000886ReturnInst::~ReturnInst() {
Devang Patel59643e52008-02-23 00:35:18 +0000887}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000888
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000889//===----------------------------------------------------------------------===//
Bill Wendlingf891bf82011-07-31 06:30:59 +0000890// ResumeInst Implementation
891//===----------------------------------------------------------------------===//
892
893ResumeInst::ResumeInst(const ResumeInst &RI)
894 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Resume,
895 OperandTraits<ResumeInst>::op_begin(this), 1) {
896 Op<0>() = RI.Op<0>();
897}
898
899ResumeInst::ResumeInst(Value *Exn, Instruction *InsertBefore)
900 : TerminatorInst(Type::getVoidTy(Exn->getContext()), Instruction::Resume,
901 OperandTraits<ResumeInst>::op_begin(this), 1, InsertBefore) {
902 Op<0>() = Exn;
903}
904
905ResumeInst::ResumeInst(Value *Exn, BasicBlock *InsertAtEnd)
906 : TerminatorInst(Type::getVoidTy(Exn->getContext()), Instruction::Resume,
907 OperandTraits<ResumeInst>::op_begin(this), 1, InsertAtEnd) {
908 Op<0>() = Exn;
909}
910
911unsigned ResumeInst::getNumSuccessorsV() const {
912 return getNumSuccessors();
913}
914
915void ResumeInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
916 llvm_unreachable("ResumeInst has no successors!");
917}
918
919BasicBlock *ResumeInst::getSuccessorV(unsigned idx) const {
920 llvm_unreachable("ResumeInst has no successors!");
Bill Wendlingf891bf82011-07-31 06:30:59 +0000921}
922
923//===----------------------------------------------------------------------===//
David Majnemer654e1302015-07-31 17:58:14 +0000924// CleanupReturnInst Implementation
925//===----------------------------------------------------------------------===//
926
927CleanupReturnInst::CleanupReturnInst(const CleanupReturnInst &CRI)
928 : TerminatorInst(CRI.getType(), Instruction::CleanupRet,
929 OperandTraits<CleanupReturnInst>::op_end(this) -
930 CRI.getNumOperands(),
931 CRI.getNumOperands()) {
David Majnemereb518bd2015-08-04 08:21:40 +0000932 setInstructionSubclassData(CRI.getSubclassDataFromInstruction());
David Majnemer8a1c45d2015-12-12 05:38:55 +0000933 Op<0>() = CRI.Op<0>();
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000934 if (CRI.hasUnwindDest())
David Majnemer8a1c45d2015-12-12 05:38:55 +0000935 Op<1>() = CRI.Op<1>();
David Majnemer654e1302015-07-31 17:58:14 +0000936}
937
David Majnemer8a1c45d2015-12-12 05:38:55 +0000938void CleanupReturnInst::init(Value *CleanupPad, BasicBlock *UnwindBB) {
David Majnemer654e1302015-07-31 17:58:14 +0000939 if (UnwindBB)
940 setInstructionSubclassData(getSubclassDataFromInstruction() | 1);
David Majnemer654e1302015-07-31 17:58:14 +0000941
David Majnemer8a1c45d2015-12-12 05:38:55 +0000942 Op<0>() = CleanupPad;
David Majnemer654e1302015-07-31 17:58:14 +0000943 if (UnwindBB)
David Majnemer8a1c45d2015-12-12 05:38:55 +0000944 Op<1>() = UnwindBB;
David Majnemer654e1302015-07-31 17:58:14 +0000945}
946
David Majnemer8a1c45d2015-12-12 05:38:55 +0000947CleanupReturnInst::CleanupReturnInst(Value *CleanupPad, BasicBlock *UnwindBB,
948 unsigned Values, Instruction *InsertBefore)
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000949 : TerminatorInst(Type::getVoidTy(CleanupPad->getContext()),
950 Instruction::CleanupRet,
David Majnemer654e1302015-07-31 17:58:14 +0000951 OperandTraits<CleanupReturnInst>::op_end(this) - Values,
952 Values, InsertBefore) {
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000953 init(CleanupPad, UnwindBB);
David Majnemer654e1302015-07-31 17:58:14 +0000954}
955
David Majnemer8a1c45d2015-12-12 05:38:55 +0000956CleanupReturnInst::CleanupReturnInst(Value *CleanupPad, BasicBlock *UnwindBB,
957 unsigned Values, BasicBlock *InsertAtEnd)
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000958 : TerminatorInst(Type::getVoidTy(CleanupPad->getContext()),
959 Instruction::CleanupRet,
David Majnemer654e1302015-07-31 17:58:14 +0000960 OperandTraits<CleanupReturnInst>::op_end(this) - Values,
961 Values, InsertAtEnd) {
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000962 init(CleanupPad, UnwindBB);
David Majnemer654e1302015-07-31 17:58:14 +0000963}
964
965BasicBlock *CleanupReturnInst::getSuccessorV(unsigned Idx) const {
966 assert(Idx == 0);
967 return getUnwindDest();
968}
969unsigned CleanupReturnInst::getNumSuccessorsV() const {
970 return getNumSuccessors();
971}
972void CleanupReturnInst::setSuccessorV(unsigned Idx, BasicBlock *B) {
973 assert(Idx == 0);
974 setUnwindDest(B);
975}
976
977//===----------------------------------------------------------------------===//
David Majnemer654e1302015-07-31 17:58:14 +0000978// CatchReturnInst Implementation
979//===----------------------------------------------------------------------===//
David Majnemer8a1c45d2015-12-12 05:38:55 +0000980void CatchReturnInst::init(Value *CatchPad, BasicBlock *BB) {
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000981 Op<0>() = CatchPad;
982 Op<1>() = BB;
David Majnemer0bc0eef2015-08-15 02:46:08 +0000983}
David Majnemer654e1302015-07-31 17:58:14 +0000984
985CatchReturnInst::CatchReturnInst(const CatchReturnInst &CRI)
986 : TerminatorInst(Type::getVoidTy(CRI.getContext()), Instruction::CatchRet,
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000987 OperandTraits<CatchReturnInst>::op_begin(this), 2) {
988 Op<0>() = CRI.Op<0>();
989 Op<1>() = CRI.Op<1>();
David Majnemer654e1302015-07-31 17:58:14 +0000990}
991
David Majnemer8a1c45d2015-12-12 05:38:55 +0000992CatchReturnInst::CatchReturnInst(Value *CatchPad, BasicBlock *BB,
David Majnemer0bc0eef2015-08-15 02:46:08 +0000993 Instruction *InsertBefore)
David Majnemer654e1302015-07-31 17:58:14 +0000994 : TerminatorInst(Type::getVoidTy(BB->getContext()), Instruction::CatchRet,
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000995 OperandTraits<CatchReturnInst>::op_begin(this), 2,
996 InsertBefore) {
997 init(CatchPad, BB);
David Majnemer654e1302015-07-31 17:58:14 +0000998}
999
David Majnemer8a1c45d2015-12-12 05:38:55 +00001000CatchReturnInst::CatchReturnInst(Value *CatchPad, BasicBlock *BB,
David Majnemer0bc0eef2015-08-15 02:46:08 +00001001 BasicBlock *InsertAtEnd)
David Majnemer654e1302015-07-31 17:58:14 +00001002 : TerminatorInst(Type::getVoidTy(BB->getContext()), Instruction::CatchRet,
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +00001003 OperandTraits<CatchReturnInst>::op_begin(this), 2,
1004 InsertAtEnd) {
1005 init(CatchPad, BB);
David Majnemer654e1302015-07-31 17:58:14 +00001006}
1007
1008BasicBlock *CatchReturnInst::getSuccessorV(unsigned Idx) const {
David Majnemerb01aa9f2015-08-23 19:22:31 +00001009 assert(Idx < getNumSuccessors() && "Successor # out of range for catchret!");
David Majnemer654e1302015-07-31 17:58:14 +00001010 return getSuccessor();
1011}
1012unsigned CatchReturnInst::getNumSuccessorsV() const {
1013 return getNumSuccessors();
1014}
1015void CatchReturnInst::setSuccessorV(unsigned Idx, BasicBlock *B) {
David Majnemerb01aa9f2015-08-23 19:22:31 +00001016 assert(Idx < getNumSuccessors() && "Successor # out of range for catchret!");
David Majnemer654e1302015-07-31 17:58:14 +00001017 setSuccessor(B);
1018}
1019
1020//===----------------------------------------------------------------------===//
David Majnemer8a1c45d2015-12-12 05:38:55 +00001021// CatchSwitchInst Implementation
David Majnemer654e1302015-07-31 17:58:14 +00001022//===----------------------------------------------------------------------===//
David Majnemer8a1c45d2015-12-12 05:38:55 +00001023
1024CatchSwitchInst::CatchSwitchInst(Value *ParentPad, BasicBlock *UnwindDest,
1025 unsigned NumReservedValues,
1026 const Twine &NameStr,
1027 Instruction *InsertBefore)
1028 : TerminatorInst(ParentPad->getType(), Instruction::CatchSwitch, nullptr, 0,
1029 InsertBefore) {
1030 if (UnwindDest)
1031 ++NumReservedValues;
1032 init(ParentPad, UnwindDest, NumReservedValues + 1);
David Majnemer654e1302015-07-31 17:58:14 +00001033 setName(NameStr);
1034}
1035
David Majnemer8a1c45d2015-12-12 05:38:55 +00001036CatchSwitchInst::CatchSwitchInst(Value *ParentPad, BasicBlock *UnwindDest,
1037 unsigned NumReservedValues,
1038 const Twine &NameStr, BasicBlock *InsertAtEnd)
1039 : TerminatorInst(ParentPad->getType(), Instruction::CatchSwitch, nullptr, 0,
David Majnemer5c73c942015-08-13 22:11:40 +00001040 InsertAtEnd) {
David Majnemer8a1c45d2015-12-12 05:38:55 +00001041 if (UnwindDest)
1042 ++NumReservedValues;
1043 init(ParentPad, UnwindDest, NumReservedValues + 1);
1044 setName(NameStr);
David Majnemer654e1302015-07-31 17:58:14 +00001045}
1046
David Majnemer8a1c45d2015-12-12 05:38:55 +00001047CatchSwitchInst::CatchSwitchInst(const CatchSwitchInst &CSI)
1048 : TerminatorInst(CSI.getType(), Instruction::CatchSwitch, nullptr,
1049 CSI.getNumOperands()) {
1050 init(CSI.getParentPad(), CSI.getUnwindDest(), CSI.getNumOperands());
1051 setNumHungOffUseOperands(ReservedSpace);
1052 Use *OL = getOperandList();
1053 const Use *InOL = CSI.getOperandList();
1054 for (unsigned I = 1, E = ReservedSpace; I != E; ++I)
1055 OL[I] = InOL[I];
David Majnemer654e1302015-07-31 17:58:14 +00001056}
David Majnemer8a1c45d2015-12-12 05:38:55 +00001057
1058void CatchSwitchInst::init(Value *ParentPad, BasicBlock *UnwindDest,
1059 unsigned NumReservedValues) {
1060 assert(ParentPad && NumReservedValues);
1061
1062 ReservedSpace = NumReservedValues;
1063 setNumHungOffUseOperands(UnwindDest ? 2 : 1);
1064 allocHungoffUses(ReservedSpace);
1065
1066 Op<0>() = ParentPad;
1067 if (UnwindDest) {
1068 setInstructionSubclassData(getSubclassDataFromInstruction() | 1);
1069 setUnwindDest(UnwindDest);
1070 }
1071}
1072
1073/// growOperands - grow operands - This grows the operand list in response to a
1074/// push_back style of operation. This grows the number of ops by 2 times.
1075void CatchSwitchInst::growOperands(unsigned Size) {
1076 unsigned NumOperands = getNumOperands();
1077 assert(NumOperands >= 1);
1078 if (ReservedSpace >= NumOperands + Size)
1079 return;
1080 ReservedSpace = (NumOperands + Size / 2) * 2;
1081 growHungoffUses(ReservedSpace);
1082}
1083
1084void CatchSwitchInst::addHandler(BasicBlock *Handler) {
1085 unsigned OpNo = getNumOperands();
1086 growOperands(1);
1087 assert(OpNo < ReservedSpace && "Growing didn't work!");
1088 setNumHungOffUseOperands(getNumOperands() + 1);
1089 getOperandList()[OpNo] = Handler;
1090}
1091
Joseph Tremoulet0d808882016-01-05 02:37:41 +00001092void CatchSwitchInst::removeHandler(handler_iterator HI) {
1093 // Move all subsequent handlers up one.
1094 Use *EndDst = op_end() - 1;
1095 for (Use *CurDst = HI.getCurrent(); CurDst != EndDst; ++CurDst)
1096 *CurDst = *(CurDst + 1);
1097 // Null out the last handler use.
1098 *EndDst = nullptr;
1099
1100 setNumHungOffUseOperands(getNumOperands() - 1);
1101}
1102
David Majnemer8a1c45d2015-12-12 05:38:55 +00001103BasicBlock *CatchSwitchInst::getSuccessorV(unsigned idx) const {
1104 return getSuccessor(idx);
1105}
1106unsigned CatchSwitchInst::getNumSuccessorsV() const {
David Majnemer654e1302015-07-31 17:58:14 +00001107 return getNumSuccessors();
1108}
David Majnemer8a1c45d2015-12-12 05:38:55 +00001109void CatchSwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
1110 setSuccessor(idx, B);
1111}
1112
1113//===----------------------------------------------------------------------===//
1114// FuncletPadInst Implementation
1115//===----------------------------------------------------------------------===//
1116void FuncletPadInst::init(Value *ParentPad, ArrayRef<Value *> Args,
1117 const Twine &NameStr) {
1118 assert(getNumOperands() == 1 + Args.size() && "NumOperands not set up?");
1119 std::copy(Args.begin(), Args.end(), op_begin());
1120 setParentPad(ParentPad);
1121 setName(NameStr);
1122}
1123
1124FuncletPadInst::FuncletPadInst(const FuncletPadInst &FPI)
1125 : Instruction(FPI.getType(), FPI.getOpcode(),
1126 OperandTraits<FuncletPadInst>::op_end(this) -
1127 FPI.getNumOperands(),
1128 FPI.getNumOperands()) {
1129 std::copy(FPI.op_begin(), FPI.op_end(), op_begin());
1130 setParentPad(FPI.getParentPad());
1131}
1132
1133FuncletPadInst::FuncletPadInst(Instruction::FuncletPadOps Op, Value *ParentPad,
1134 ArrayRef<Value *> Args, unsigned Values,
1135 const Twine &NameStr, Instruction *InsertBefore)
1136 : Instruction(ParentPad->getType(), Op,
1137 OperandTraits<FuncletPadInst>::op_end(this) - Values, Values,
1138 InsertBefore) {
1139 init(ParentPad, Args, NameStr);
1140}
1141
1142FuncletPadInst::FuncletPadInst(Instruction::FuncletPadOps Op, Value *ParentPad,
1143 ArrayRef<Value *> Args, unsigned Values,
1144 const Twine &NameStr, BasicBlock *InsertAtEnd)
1145 : Instruction(ParentPad->getType(), Op,
1146 OperandTraits<FuncletPadInst>::op_end(this) - Values, Values,
1147 InsertAtEnd) {
1148 init(ParentPad, Args, NameStr);
David Majnemer654e1302015-07-31 17:58:14 +00001149}
1150
1151//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +00001152// UnreachableInst Implementation
1153//===----------------------------------------------------------------------===//
1154
Owen Anderson55f1c092009-08-13 21:58:54 +00001155UnreachableInst::UnreachableInst(LLVMContext &Context,
1156 Instruction *InsertBefore)
1157 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
Craig Topperc6207612014-04-09 06:08:46 +00001158 nullptr, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +00001159}
Owen Anderson55f1c092009-08-13 21:58:54 +00001160UnreachableInst::UnreachableInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
1161 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
Craig Topperc6207612014-04-09 06:08:46 +00001162 nullptr, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +00001163}
1164
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001165unsigned UnreachableInst::getNumSuccessorsV() const {
1166 return getNumSuccessors();
1167}
1168
1169void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Bill Wendling0aef16a2012-02-06 21:44:22 +00001170 llvm_unreachable("UnreachableInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001171}
1172
1173BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
Bill Wendling0aef16a2012-02-06 21:44:22 +00001174 llvm_unreachable("UnreachableInst has no successors!");
Chris Lattner5e0b9f22004-10-16 18:08:06 +00001175}
1176
1177//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001178// BranchInst Implementation
1179//===----------------------------------------------------------------------===//
1180
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001181void BranchInst::AssertOK() {
1182 if (isConditional())
Duncan Sands9dff9be2010-02-15 16:12:20 +00001183 assert(getCondition()->getType()->isIntegerTy(1) &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001184 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001185}
1186
Chris Lattner2195fc42007-02-24 00:55:48 +00001187BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001188 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +00001189 OperandTraits<BranchInst>::op_end(this) - 1,
1190 1, InsertBefore) {
Craig Topper2617dcc2014-04-15 06:32:26 +00001191 assert(IfTrue && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001192 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +00001193}
1194BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
1195 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001196 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +00001197 OperandTraits<BranchInst>::op_end(this) - 3,
1198 3, InsertBefore) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001199 Op<-1>() = IfTrue;
1200 Op<-2>() = IfFalse;
1201 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +00001202#ifndef NDEBUG
1203 AssertOK();
1204#endif
1205}
1206
1207BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001208 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +00001209 OperandTraits<BranchInst>::op_end(this) - 1,
1210 1, InsertAtEnd) {
Craig Topper2617dcc2014-04-15 06:32:26 +00001211 assert(IfTrue && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001212 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +00001213}
1214
1215BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
1216 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001217 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +00001218 OperandTraits<BranchInst>::op_end(this) - 3,
1219 3, InsertAtEnd) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001220 Op<-1>() = IfTrue;
1221 Op<-2>() = IfFalse;
1222 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +00001223#ifndef NDEBUG
1224 AssertOK();
1225#endif
1226}
1227
1228
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001229BranchInst::BranchInst(const BranchInst &BI) :
Owen Anderson55f1c092009-08-13 21:58:54 +00001230 TerminatorInst(Type::getVoidTy(BI.getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +00001231 OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(),
1232 BI.getNumOperands()) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001233 Op<-1>() = BI.Op<-1>();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001234 if (BI.getNumOperands() != 1) {
1235 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001236 Op<-3>() = BI.Op<-3>();
1237 Op<-2>() = BI.Op<-2>();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001238 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001239 SubclassOptionalData = BI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001240}
1241
Chandler Carruth3e8aa652011-10-17 01:11:57 +00001242void BranchInst::swapSuccessors() {
1243 assert(isConditional() &&
1244 "Cannot swap successors of an unconditional branch");
1245 Op<-1>().swap(Op<-2>());
1246
1247 // Update profile metadata if present and it matches our structural
1248 // expectations.
Xinliang David Lidc491402016-08-23 15:39:03 +00001249 swapProfMetadata();
Chandler Carruth3e8aa652011-10-17 01:11:57 +00001250}
1251
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001252BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
1253 return getSuccessor(idx);
1254}
1255unsigned BranchInst::getNumSuccessorsV() const {
1256 return getNumSuccessors();
1257}
1258void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
1259 setSuccessor(idx, B);
1260}
1261
1262
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001263//===----------------------------------------------------------------------===//
Victor Hernandez8acf2952009-10-23 21:09:37 +00001264// AllocaInst Implementation
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001265//===----------------------------------------------------------------------===//
1266
Owen Andersonb6b25302009-07-14 23:09:55 +00001267static Value *getAISize(LLVMContext &Context, Value *Amt) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001268 if (!Amt)
Owen Anderson55f1c092009-08-13 21:58:54 +00001269 Amt = ConstantInt::get(Type::getInt32Ty(Context), 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +00001270 else {
1271 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +00001272 "Passed basic block into allocation size parameter! Use other ctor");
Dan Gohman2140a742010-05-28 01:14:11 +00001273 assert(Amt->getType()->isIntegerTy() &&
1274 "Allocation array size is not an integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +00001275 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001276 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001277}
1278
Matt Arsenault3c1fc762017-04-10 22:27:50 +00001279AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, const Twine &Name,
Victor Hernandez8acf2952009-10-23 21:09:37 +00001280 Instruction *InsertBefore)
Matt Arsenault3c1fc762017-04-10 22:27:50 +00001281 : AllocaInst(Ty, AddrSpace, /*ArraySize=*/nullptr, Name, InsertBefore) {}
Victor Hernandez8acf2952009-10-23 21:09:37 +00001282
Matt Arsenault3c1fc762017-04-10 22:27:50 +00001283AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, const Twine &Name,
Victor Hernandez8acf2952009-10-23 21:09:37 +00001284 BasicBlock *InsertAtEnd)
Matt Arsenault3c1fc762017-04-10 22:27:50 +00001285 : AllocaInst(Ty, AddrSpace, /*ArraySize=*/nullptr, Name, InsertAtEnd) {}
Victor Hernandez8acf2952009-10-23 21:09:37 +00001286
Matt Arsenault3c1fc762017-04-10 22:27:50 +00001287AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize,
Victor Hernandez8acf2952009-10-23 21:09:37 +00001288 const Twine &Name, Instruction *InsertBefore)
Matt Arsenault3c1fc762017-04-10 22:27:50 +00001289 : AllocaInst(Ty, AddrSpace, ArraySize, /*Align=*/0, Name, InsertBefore) {}
1290
1291AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize,
1292 const Twine &Name, BasicBlock *InsertAtEnd)
1293 : AllocaInst(Ty, AddrSpace, ArraySize, /*Align=*/0, Name, InsertAtEnd) {}
1294
1295AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize,
1296 unsigned Align, const Twine &Name,
1297 Instruction *InsertBefore)
1298 : UnaryInstruction(PointerType::get(Ty, AddrSpace), Alloca,
1299 getAISize(Ty->getContext(), ArraySize), InsertBefore),
1300 AllocatedType(Ty) {
Dan Gohmanaa583d72008-03-24 16:55:58 +00001301 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +00001302 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +00001303 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001304}
1305
Matt Arsenault3c1fc762017-04-10 22:27:50 +00001306AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize,
1307 unsigned Align, const Twine &Name,
1308 BasicBlock *InsertAtEnd)
1309 : UnaryInstruction(PointerType::get(Ty, AddrSpace), Alloca,
1310 getAISize(Ty->getContext(), ArraySize), InsertAtEnd),
David Blaikiebf0a42a2015-04-29 23:00:35 +00001311 AllocatedType(Ty) {
Dan Gohmanaa583d72008-03-24 16:55:58 +00001312 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +00001313 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +00001314 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001315}
1316
Gordon Henriksen14a55692007-12-10 02:14:30 +00001317// Out of line virtual method, so the vtable, etc has a home.
Victor Hernandez8acf2952009-10-23 21:09:37 +00001318AllocaInst::~AllocaInst() {
Gordon Henriksen14a55692007-12-10 02:14:30 +00001319}
1320
Victor Hernandez8acf2952009-10-23 21:09:37 +00001321void AllocaInst::setAlignment(unsigned Align) {
Dan Gohmanaa583d72008-03-24 16:55:58 +00001322 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001323 assert(Align <= MaximumAlignment &&
1324 "Alignment is greater than MaximumAlignment!");
Reid Kleckner436c42e2014-01-17 23:58:17 +00001325 setInstructionSubclassData((getSubclassDataFromInstruction() & ~31) |
1326 (Log2_32(Align) + 1));
Dan Gohmanaa583d72008-03-24 16:55:58 +00001327 assert(getAlignment() == Align && "Alignment representation error!");
1328}
1329
Victor Hernandez8acf2952009-10-23 21:09:37 +00001330bool AllocaInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +00001331 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
Dan Gohman9a1b8592010-09-27 15:15:44 +00001332 return !CI->isOne();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001333 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001334}
1335
Chris Lattner8b291e62008-11-26 02:54:17 +00001336/// isStaticAlloca - Return true if this alloca is in the entry block of the
1337/// function and is a constant size. If so, the code generator will fold it
1338/// into the prolog/epilog code, so it is basically free.
1339bool AllocaInst::isStaticAlloca() const {
1340 // Must be constant size.
1341 if (!isa<ConstantInt>(getArraySize())) return false;
1342
1343 // Must be in the entry block.
1344 const BasicBlock *Parent = getParent();
Reid Kleckner436c42e2014-01-17 23:58:17 +00001345 return Parent == &Parent->getParent()->front() && !isUsedWithInAlloca();
Chris Lattner8b291e62008-11-26 02:54:17 +00001346}
1347
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001348//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001349// LoadInst Implementation
1350//===----------------------------------------------------------------------===//
1351
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001352void LoadInst::AssertOK() {
Duncan Sands19d0b472010-02-16 11:11:14 +00001353 assert(getOperand(0)->getType()->isPointerTy() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001354 "Ptr must have pointer type.");
Eli Friedman59b66882011-08-09 23:02:53 +00001355 assert(!(isAtomic() && getAlignment() == 0) &&
1356 "Alignment required for atomic load");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001357}
1358
Daniel Dunbar4975db62009-07-25 04:41:11 +00001359LoadInst::LoadInst(Value *Ptr, const Twine &Name, Instruction *InsertBef)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001360 : LoadInst(Ptr, Name, /*isVolatile=*/false, InsertBef) {}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001361
Daniel Dunbar4975db62009-07-25 04:41:11 +00001362LoadInst::LoadInst(Value *Ptr, const Twine &Name, BasicBlock *InsertAE)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001363 : LoadInst(Ptr, Name, /*isVolatile=*/false, InsertAE) {}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001364
David Blaikie0c28fd72015-05-20 21:46:30 +00001365LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001366 Instruction *InsertBef)
David Blaikie0c28fd72015-05-20 21:46:30 +00001367 : LoadInst(Ty, Ptr, Name, isVolatile, /*Align=*/0, InsertBef) {}
Eli Friedman59b66882011-08-09 23:02:53 +00001368
1369LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
1370 BasicBlock *InsertAE)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001371 : LoadInst(Ptr, Name, isVolatile, /*Align=*/0, InsertAE) {}
Christopher Lamb84485702007-04-22 19:24:39 +00001372
David Blaikieb7a029872015-04-17 19:56:21 +00001373LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Christopher Lamb84485702007-04-22 19:24:39 +00001374 unsigned Align, Instruction *InsertBef)
JF Bastien800f87a2016-04-06 21:19:33 +00001375 : LoadInst(Ty, Ptr, Name, isVolatile, Align, AtomicOrdering::NotAtomic,
1376 CrossThread, InsertBef) {}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001377
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001378LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +00001379 unsigned Align, BasicBlock *InsertAE)
JF Bastien800f87a2016-04-06 21:19:33 +00001380 : LoadInst(Ptr, Name, isVolatile, Align, AtomicOrdering::NotAtomic,
1381 CrossThread, InsertAE) {}
Dan Gohman68659282007-07-18 20:51:11 +00001382
David Blaikie15d9a4c2015-04-06 20:59:48 +00001383LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001384 unsigned Align, AtomicOrdering Order,
David Blaikie15d9a4c2015-04-06 20:59:48 +00001385 SynchronizationScope SynchScope, Instruction *InsertBef)
1386 : UnaryInstruction(Ty, Load, Ptr, InsertBef) {
David Blaikie79009f82015-05-20 20:22:31 +00001387 assert(Ty == cast<PointerType>(Ptr->getType())->getElementType());
Eli Friedman59b66882011-08-09 23:02:53 +00001388 setVolatile(isVolatile);
1389 setAlignment(Align);
1390 setAtomic(Order, SynchScope);
1391 AssertOK();
1392 setName(Name);
1393}
1394
1395LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
1396 unsigned Align, AtomicOrdering Order,
1397 SynchronizationScope SynchScope,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001398 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001399 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001400 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +00001401 setVolatile(isVolatile);
Eli Friedman59b66882011-08-09 23:02:53 +00001402 setAlignment(Align);
1403 setAtomic(Order, SynchScope);
Chris Lattner0f048162007-02-13 07:54:42 +00001404 AssertOK();
1405 setName(Name);
1406}
1407
Daniel Dunbar27096822009-08-11 18:11:15 +00001408LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
1409 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1410 Load, Ptr, InsertBef) {
1411 setVolatile(false);
1412 setAlignment(0);
JF Bastien800f87a2016-04-06 21:19:33 +00001413 setAtomic(AtomicOrdering::NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001414 AssertOK();
1415 if (Name && Name[0]) setName(Name);
1416}
1417
1418LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
1419 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1420 Load, Ptr, InsertAE) {
1421 setVolatile(false);
1422 setAlignment(0);
JF Bastien800f87a2016-04-06 21:19:33 +00001423 setAtomic(AtomicOrdering::NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001424 AssertOK();
1425 if (Name && Name[0]) setName(Name);
1426}
1427
David Blaikie0c28fd72015-05-20 21:46:30 +00001428LoadInst::LoadInst(Type *Ty, Value *Ptr, const char *Name, bool isVolatile,
Daniel Dunbar27096822009-08-11 18:11:15 +00001429 Instruction *InsertBef)
David Blaikie0c28fd72015-05-20 21:46:30 +00001430 : UnaryInstruction(Ty, Load, Ptr, InsertBef) {
1431 assert(Ty == cast<PointerType>(Ptr->getType())->getElementType());
Daniel Dunbar27096822009-08-11 18:11:15 +00001432 setVolatile(isVolatile);
1433 setAlignment(0);
JF Bastien800f87a2016-04-06 21:19:33 +00001434 setAtomic(AtomicOrdering::NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001435 AssertOK();
1436 if (Name && Name[0]) setName(Name);
1437}
1438
1439LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
1440 BasicBlock *InsertAE)
1441 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1442 Load, Ptr, InsertAE) {
1443 setVolatile(isVolatile);
1444 setAlignment(0);
JF Bastien800f87a2016-04-06 21:19:33 +00001445 setAtomic(AtomicOrdering::NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001446 AssertOK();
1447 if (Name && Name[0]) setName(Name);
1448}
1449
Christopher Lamb84485702007-04-22 19:24:39 +00001450void LoadInst::setAlignment(unsigned Align) {
1451 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001452 assert(Align <= MaximumAlignment &&
1453 "Alignment is greater than MaximumAlignment!");
Eli Friedman59b66882011-08-09 23:02:53 +00001454 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) |
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001455 ((Log2_32(Align)+1)<<1));
Dan Gohmana7e5a242010-07-28 20:12:04 +00001456 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +00001457}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001458
1459//===----------------------------------------------------------------------===//
1460// StoreInst Implementation
1461//===----------------------------------------------------------------------===//
1462
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001463void StoreInst::AssertOK() {
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001464 assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001465 assert(getOperand(1)->getType()->isPointerTy() &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001466 "Ptr must have pointer type!");
1467 assert(getOperand(0)->getType() ==
1468 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +00001469 && "Ptr must be a pointer to Val type!");
Eli Friedman59b66882011-08-09 23:02:53 +00001470 assert(!(isAtomic() && getAlignment() == 0) &&
Mark Lacey1d7c97e2013-12-21 00:00:49 +00001471 "Alignment required for atomic store");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001472}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001473
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001474StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001475 : StoreInst(val, addr, /*isVolatile=*/false, InsertBefore) {}
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001476
1477StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001478 : StoreInst(val, addr, /*isVolatile=*/false, InsertAtEnd) {}
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001479
Misha Brukmanb1c93172005-04-21 23:48:37 +00001480StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001481 Instruction *InsertBefore)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001482 : StoreInst(val, addr, isVolatile, /*Align=*/0, InsertBefore) {}
Christopher Lamb84485702007-04-22 19:24:39 +00001483
1484StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001485 BasicBlock *InsertAtEnd)
1486 : StoreInst(val, addr, isVolatile, /*Align=*/0, InsertAtEnd) {}
1487
1488StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, unsigned Align,
1489 Instruction *InsertBefore)
JF Bastien800f87a2016-04-06 21:19:33 +00001490 : StoreInst(val, addr, isVolatile, Align, AtomicOrdering::NotAtomic,
1491 CrossThread, InsertBefore) {}
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001492
1493StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, unsigned Align,
1494 BasicBlock *InsertAtEnd)
JF Bastien800f87a2016-04-06 21:19:33 +00001495 : StoreInst(val, addr, isVolatile, Align, AtomicOrdering::NotAtomic,
1496 CrossThread, InsertAtEnd) {}
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001497
Misha Brukmanb1c93172005-04-21 23:48:37 +00001498StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001499 unsigned Align, AtomicOrdering Order,
1500 SynchronizationScope SynchScope,
1501 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001502 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001503 OperandTraits<StoreInst>::op_begin(this),
1504 OperandTraits<StoreInst>::operands(this),
Eli Friedman59b66882011-08-09 23:02:53 +00001505 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001506 Op<0>() = val;
1507 Op<1>() = addr;
Dan Gohman68659282007-07-18 20:51:11 +00001508 setVolatile(isVolatile);
1509 setAlignment(Align);
Eli Friedman59b66882011-08-09 23:02:53 +00001510 setAtomic(Order, SynchScope);
Dan Gohman68659282007-07-18 20:51:11 +00001511 AssertOK();
1512}
1513
1514StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001515 unsigned Align, AtomicOrdering Order,
1516 SynchronizationScope SynchScope,
1517 BasicBlock *InsertAtEnd)
1518 : Instruction(Type::getVoidTy(val->getContext()), Store,
1519 OperandTraits<StoreInst>::op_begin(this),
1520 OperandTraits<StoreInst>::operands(this),
1521 InsertAtEnd) {
1522 Op<0>() = val;
1523 Op<1>() = addr;
1524 setVolatile(isVolatile);
1525 setAlignment(Align);
1526 setAtomic(Order, SynchScope);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001527 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001528}
1529
Christopher Lamb84485702007-04-22 19:24:39 +00001530void StoreInst::setAlignment(unsigned Align) {
1531 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001532 assert(Align <= MaximumAlignment &&
1533 "Alignment is greater than MaximumAlignment!");
Eli Friedman59b66882011-08-09 23:02:53 +00001534 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) |
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001535 ((Log2_32(Align)+1) << 1));
Dan Gohmana7e5a242010-07-28 20:12:04 +00001536 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +00001537}
1538
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001539//===----------------------------------------------------------------------===//
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001540// AtomicCmpXchgInst Implementation
1541//===----------------------------------------------------------------------===//
1542
1543void AtomicCmpXchgInst::Init(Value *Ptr, Value *Cmp, Value *NewVal,
Tim Northovere94a5182014-03-11 10:48:52 +00001544 AtomicOrdering SuccessOrdering,
1545 AtomicOrdering FailureOrdering,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001546 SynchronizationScope SynchScope) {
1547 Op<0>() = Ptr;
1548 Op<1>() = Cmp;
1549 Op<2>() = NewVal;
Tim Northovere94a5182014-03-11 10:48:52 +00001550 setSuccessOrdering(SuccessOrdering);
1551 setFailureOrdering(FailureOrdering);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001552 setSynchScope(SynchScope);
1553
1554 assert(getOperand(0) && getOperand(1) && getOperand(2) &&
1555 "All operands must be non-null!");
1556 assert(getOperand(0)->getType()->isPointerTy() &&
1557 "Ptr must have pointer type!");
1558 assert(getOperand(1)->getType() ==
1559 cast<PointerType>(getOperand(0)->getType())->getElementType()
1560 && "Ptr must be a pointer to Cmp type!");
1561 assert(getOperand(2)->getType() ==
1562 cast<PointerType>(getOperand(0)->getType())->getElementType()
1563 && "Ptr must be a pointer to NewVal type!");
JF Bastien800f87a2016-04-06 21:19:33 +00001564 assert(SuccessOrdering != AtomicOrdering::NotAtomic &&
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001565 "AtomicCmpXchg instructions must be atomic!");
JF Bastien800f87a2016-04-06 21:19:33 +00001566 assert(FailureOrdering != AtomicOrdering::NotAtomic &&
Tim Northovere94a5182014-03-11 10:48:52 +00001567 "AtomicCmpXchg instructions must be atomic!");
JF Bastien800f87a2016-04-06 21:19:33 +00001568 assert(!isStrongerThan(FailureOrdering, SuccessOrdering) &&
1569 "AtomicCmpXchg failure argument shall be no stronger than the success "
1570 "argument");
1571 assert(FailureOrdering != AtomicOrdering::Release &&
1572 FailureOrdering != AtomicOrdering::AcquireRelease &&
Tim Northovere94a5182014-03-11 10:48:52 +00001573 "AtomicCmpXchg failure ordering cannot include release semantics");
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001574}
1575
1576AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
Tim Northovere94a5182014-03-11 10:48:52 +00001577 AtomicOrdering SuccessOrdering,
1578 AtomicOrdering FailureOrdering,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001579 SynchronizationScope SynchScope,
1580 Instruction *InsertBefore)
Tim Northover420a2162014-06-13 14:24:07 +00001581 : Instruction(
Serge Gueltone38003f2017-05-09 19:31:13 +00001582 StructType::get(Cmp->getType(), Type::getInt1Ty(Cmp->getContext())),
Tim Northover420a2162014-06-13 14:24:07 +00001583 AtomicCmpXchg, OperandTraits<AtomicCmpXchgInst>::op_begin(this),
1584 OperandTraits<AtomicCmpXchgInst>::operands(this), InsertBefore) {
Tim Northovere94a5182014-03-11 10:48:52 +00001585 Init(Ptr, Cmp, NewVal, SuccessOrdering, FailureOrdering, SynchScope);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001586}
1587
1588AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
Tim Northovere94a5182014-03-11 10:48:52 +00001589 AtomicOrdering SuccessOrdering,
1590 AtomicOrdering FailureOrdering,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001591 SynchronizationScope SynchScope,
1592 BasicBlock *InsertAtEnd)
Tim Northover420a2162014-06-13 14:24:07 +00001593 : Instruction(
Serge Gueltone38003f2017-05-09 19:31:13 +00001594 StructType::get(Cmp->getType(), Type::getInt1Ty(Cmp->getContext())),
Tim Northover420a2162014-06-13 14:24:07 +00001595 AtomicCmpXchg, OperandTraits<AtomicCmpXchgInst>::op_begin(this),
1596 OperandTraits<AtomicCmpXchgInst>::operands(this), InsertAtEnd) {
Tim Northovere94a5182014-03-11 10:48:52 +00001597 Init(Ptr, Cmp, NewVal, SuccessOrdering, FailureOrdering, SynchScope);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001598}
Tim Northover420a2162014-06-13 14:24:07 +00001599
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001600//===----------------------------------------------------------------------===//
1601// AtomicRMWInst Implementation
1602//===----------------------------------------------------------------------===//
1603
1604void AtomicRMWInst::Init(BinOp Operation, Value *Ptr, Value *Val,
1605 AtomicOrdering Ordering,
1606 SynchronizationScope SynchScope) {
1607 Op<0>() = Ptr;
1608 Op<1>() = Val;
1609 setOperation(Operation);
1610 setOrdering(Ordering);
1611 setSynchScope(SynchScope);
1612
1613 assert(getOperand(0) && getOperand(1) &&
1614 "All operands must be non-null!");
1615 assert(getOperand(0)->getType()->isPointerTy() &&
1616 "Ptr must have pointer type!");
1617 assert(getOperand(1)->getType() ==
1618 cast<PointerType>(getOperand(0)->getType())->getElementType()
1619 && "Ptr must be a pointer to Val type!");
JF Bastien800f87a2016-04-06 21:19:33 +00001620 assert(Ordering != AtomicOrdering::NotAtomic &&
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001621 "AtomicRMW instructions must be atomic!");
1622}
1623
1624AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
1625 AtomicOrdering Ordering,
1626 SynchronizationScope SynchScope,
1627 Instruction *InsertBefore)
1628 : Instruction(Val->getType(), AtomicRMW,
1629 OperandTraits<AtomicRMWInst>::op_begin(this),
1630 OperandTraits<AtomicRMWInst>::operands(this),
1631 InsertBefore) {
1632 Init(Operation, Ptr, Val, Ordering, SynchScope);
1633}
1634
1635AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
1636 AtomicOrdering Ordering,
1637 SynchronizationScope SynchScope,
1638 BasicBlock *InsertAtEnd)
1639 : Instruction(Val->getType(), AtomicRMW,
1640 OperandTraits<AtomicRMWInst>::op_begin(this),
1641 OperandTraits<AtomicRMWInst>::operands(this),
1642 InsertAtEnd) {
1643 Init(Operation, Ptr, Val, Ordering, SynchScope);
1644}
1645
1646//===----------------------------------------------------------------------===//
Eli Friedmanfee02c62011-07-25 23:16:38 +00001647// FenceInst Implementation
1648//===----------------------------------------------------------------------===//
1649
1650FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering,
1651 SynchronizationScope SynchScope,
1652 Instruction *InsertBefore)
Craig Topperc6207612014-04-09 06:08:46 +00001653 : Instruction(Type::getVoidTy(C), Fence, nullptr, 0, InsertBefore) {
Eli Friedmanfee02c62011-07-25 23:16:38 +00001654 setOrdering(Ordering);
1655 setSynchScope(SynchScope);
1656}
1657
1658FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering,
1659 SynchronizationScope SynchScope,
1660 BasicBlock *InsertAtEnd)
Craig Topperc6207612014-04-09 06:08:46 +00001661 : Instruction(Type::getVoidTy(C), Fence, nullptr, 0, InsertAtEnd) {
Eli Friedmanfee02c62011-07-25 23:16:38 +00001662 setOrdering(Ordering);
1663 setSynchScope(SynchScope);
1664}
1665
1666//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001667// GetElementPtrInst Implementation
1668//===----------------------------------------------------------------------===//
1669
Pete Cooper1d078692015-12-14 20:29:16 +00001670void GetElementPtrInst::anchor() {}
1671
Jay Foadd1b78492011-07-25 09:48:08 +00001672void GetElementPtrInst::init(Value *Ptr, ArrayRef<Value *> IdxList,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001673 const Twine &Name) {
Pete Cooperb4eede22015-06-12 17:48:10 +00001674 assert(getNumOperands() == 1 + IdxList.size() &&
1675 "NumOperands not initialized?");
Pete Coopereb31b682015-05-21 22:48:54 +00001676 Op<0>() = Ptr;
Jay Foadd1b78492011-07-25 09:48:08 +00001677 std::copy(IdxList.begin(), IdxList.end(), op_begin() + 1);
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001678 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001679}
1680
Gabor Greiff6caff662008-05-10 08:32:32 +00001681GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
David Blaikie73cf8722015-05-05 18:03:48 +00001682 : Instruction(GEPI.getType(), GetElementPtr,
1683 OperandTraits<GetElementPtrInst>::op_end(this) -
1684 GEPI.getNumOperands(),
1685 GEPI.getNumOperands()),
David Blaikief5147ef2015-06-01 03:09:34 +00001686 SourceElementType(GEPI.SourceElementType),
1687 ResultElementType(GEPI.ResultElementType) {
Jay Foadd1b78492011-07-25 09:48:08 +00001688 std::copy(GEPI.op_begin(), GEPI.op_end(), op_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001689 SubclassOptionalData = GEPI.SubclassOptionalData;
Gabor Greiff6caff662008-05-10 08:32:32 +00001690}
1691
Chris Lattner6090a422009-03-09 04:46:40 +00001692/// getIndexedType - Returns the type of the element that would be accessed with
1693/// a gep instruction with the specified parameters.
1694///
1695/// The Idxs pointer should point to a continuous piece of memory containing the
1696/// indices, either as Value* or uint64_t.
1697///
1698/// A null type is returned if the indices are invalid for the specified
1699/// pointer type.
1700///
Matthijs Kooijman04468622008-07-29 08:46:11 +00001701template <typename IndexTy>
David Blaikied288fb82015-03-30 21:41:43 +00001702static Type *getIndexedTypeInternal(Type *Agg, ArrayRef<IndexTy> IdxList) {
Chris Lattner6090a422009-03-09 04:46:40 +00001703 // Handle the special case of the empty set index set, which is always valid.
Jay Foadd1b78492011-07-25 09:48:08 +00001704 if (IdxList.empty())
Dan Gohman12fce772008-05-15 19:50:34 +00001705 return Agg;
Nadav Rotem3924cb02011-12-05 06:29:09 +00001706
Chris Lattner6090a422009-03-09 04:46:40 +00001707 // If there is at least one index, the top level type must be sized, otherwise
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001708 // it cannot be 'stepped over'.
1709 if (!Agg->isSized())
Craig Topperc6207612014-04-09 06:08:46 +00001710 return nullptr;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001711
Dan Gohman1ecaf452008-05-31 00:58:22 +00001712 unsigned CurIdx = 1;
Jay Foadd1b78492011-07-25 09:48:08 +00001713 for (; CurIdx != IdxList.size(); ++CurIdx) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001714 CompositeType *CT = dyn_cast<CompositeType>(Agg);
Craig Topperc6207612014-04-09 06:08:46 +00001715 if (!CT || CT->isPointerTy()) return nullptr;
Jay Foadd1b78492011-07-25 09:48:08 +00001716 IndexTy Index = IdxList[CurIdx];
Craig Topperc6207612014-04-09 06:08:46 +00001717 if (!CT->indexValid(Index)) return nullptr;
Dan Gohman1ecaf452008-05-31 00:58:22 +00001718 Agg = CT->getTypeAtIndex(Index);
Dan Gohman1ecaf452008-05-31 00:58:22 +00001719 }
Craig Topperc6207612014-04-09 06:08:46 +00001720 return CurIdx == IdxList.size() ? Agg : nullptr;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001721}
1722
David Blaikied288fb82015-03-30 21:41:43 +00001723Type *GetElementPtrInst::getIndexedType(Type *Ty, ArrayRef<Value *> IdxList) {
1724 return getIndexedTypeInternal(Ty, IdxList);
Matthijs Kooijman04468622008-07-29 08:46:11 +00001725}
1726
David Blaikied288fb82015-03-30 21:41:43 +00001727Type *GetElementPtrInst::getIndexedType(Type *Ty,
Jay Foadd1b78492011-07-25 09:48:08 +00001728 ArrayRef<Constant *> IdxList) {
David Blaikied288fb82015-03-30 21:41:43 +00001729 return getIndexedTypeInternal(Ty, IdxList);
Jay Foad1d4a8fe2011-01-14 08:07:43 +00001730}
1731
David Blaikied288fb82015-03-30 21:41:43 +00001732Type *GetElementPtrInst::getIndexedType(Type *Ty, ArrayRef<uint64_t> IdxList) {
1733 return getIndexedTypeInternal(Ty, IdxList);
Matthijs Kooijman04468622008-07-29 08:46:11 +00001734}
1735
Chris Lattner45f15572007-04-14 00:12:57 +00001736/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1737/// zeros. If so, the result pointer and the first operand have the same
1738/// value, just potentially different types.
1739bool GetElementPtrInst::hasAllZeroIndices() const {
1740 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1741 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1742 if (!CI->isZero()) return false;
1743 } else {
1744 return false;
1745 }
1746 }
1747 return true;
1748}
1749
Chris Lattner27058292007-04-27 20:35:56 +00001750/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1751/// constant integers. If so, the result pointer and the first operand have
1752/// a constant offset between them.
1753bool GetElementPtrInst::hasAllConstantIndices() const {
1754 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1755 if (!isa<ConstantInt>(getOperand(i)))
1756 return false;
1757 }
1758 return true;
1759}
1760
Dan Gohman1b849082009-09-07 23:54:19 +00001761void GetElementPtrInst::setIsInBounds(bool B) {
1762 cast<GEPOperator>(this)->setIsInBounds(B);
1763}
Chris Lattner45f15572007-04-14 00:12:57 +00001764
Nick Lewycky28a5f252009-09-27 21:33:04 +00001765bool GetElementPtrInst::isInBounds() const {
1766 return cast<GEPOperator>(this)->isInBounds();
1767}
1768
Chandler Carruth1e140532012-12-11 10:29:10 +00001769bool GetElementPtrInst::accumulateConstantOffset(const DataLayout &DL,
1770 APInt &Offset) const {
Chandler Carruth7ec41c72012-12-11 11:05:15 +00001771 // Delegate to the generic GEPOperator implementation.
1772 return cast<GEPOperator>(this)->accumulateConstantOffset(DL, Offset);
Chandler Carruth1e140532012-12-11 10:29:10 +00001773}
1774
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001775//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001776// ExtractElementInst Implementation
1777//===----------------------------------------------------------------------===//
1778
1779ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001780 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001781 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001782 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001783 ExtractElement,
1784 OperandTraits<ExtractElementInst>::op_begin(this),
1785 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001786 assert(isValidOperands(Val, Index) &&
1787 "Invalid extractelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001788 Op<0>() = Val;
1789 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001790 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001791}
1792
1793ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001794 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001795 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001796 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001797 ExtractElement,
1798 OperandTraits<ExtractElementInst>::op_begin(this),
1799 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001800 assert(isValidOperands(Val, Index) &&
1801 "Invalid extractelement instruction operands!");
1802
Gabor Greif2d3024d2008-05-26 21:33:52 +00001803 Op<0>() = Val;
1804 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001805 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001806}
1807
Chris Lattner65511ff2006-10-05 06:24:58 +00001808
Chris Lattner54865b32006-04-08 04:05:48 +00001809bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00001810 if (!Val->getType()->isVectorTy() || !Index->getType()->isIntegerTy())
Chris Lattner54865b32006-04-08 04:05:48 +00001811 return false;
1812 return true;
1813}
1814
1815
Robert Bocchino23004482006-01-10 19:05:34 +00001816//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001817// InsertElementInst Implementation
1818//===----------------------------------------------------------------------===//
1819
Chris Lattner54865b32006-04-08 04:05:48 +00001820InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001821 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001822 Instruction *InsertBef)
Gabor Greiff6caff662008-05-10 08:32:32 +00001823 : Instruction(Vec->getType(), InsertElement,
1824 OperandTraits<InsertElementInst>::op_begin(this),
1825 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001826 assert(isValidOperands(Vec, Elt, Index) &&
1827 "Invalid insertelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001828 Op<0>() = Vec;
1829 Op<1>() = Elt;
1830 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001831 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001832}
1833
Chris Lattner54865b32006-04-08 04:05:48 +00001834InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001835 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001836 BasicBlock *InsertAE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001837 : Instruction(Vec->getType(), InsertElement,
1838 OperandTraits<InsertElementInst>::op_begin(this),
1839 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001840 assert(isValidOperands(Vec, Elt, Index) &&
1841 "Invalid insertelement instruction operands!");
1842
Gabor Greif2d3024d2008-05-26 21:33:52 +00001843 Op<0>() = Vec;
1844 Op<1>() = Elt;
1845 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001846 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001847}
1848
Chris Lattner54865b32006-04-08 04:05:48 +00001849bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1850 const Value *Index) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001851 if (!Vec->getType()->isVectorTy())
Reid Spencer09575ba2007-02-15 03:39:18 +00001852 return false; // First operand of insertelement must be vector type.
Chris Lattner54865b32006-04-08 04:05:48 +00001853
Reid Spencerd84d35b2007-02-15 02:26:10 +00001854 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001855 return false;// Second operand of insertelement must be vector element type.
Chris Lattner54865b32006-04-08 04:05:48 +00001856
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00001857 if (!Index->getType()->isIntegerTy())
Dan Gohman4fe64de2009-06-14 23:30:43 +00001858 return false; // Third operand of insertelement must be i32.
Chris Lattner54865b32006-04-08 04:05:48 +00001859 return true;
1860}
1861
1862
Robert Bocchinoca27f032006-01-17 20:07:22 +00001863//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001864// ShuffleVectorInst Implementation
1865//===----------------------------------------------------------------------===//
1866
1867ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001868 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001869 Instruction *InsertBefore)
Owen Anderson4056ca92009-07-29 22:17:13 +00001870: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
Mon P Wang25f01062008-11-10 04:46:22 +00001871 cast<VectorType>(Mask->getType())->getNumElements()),
1872 ShuffleVector,
1873 OperandTraits<ShuffleVectorInst>::op_begin(this),
1874 OperandTraits<ShuffleVectorInst>::operands(this),
1875 InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001876 assert(isValidOperands(V1, V2, Mask) &&
1877 "Invalid shuffle vector instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001878 Op<0>() = V1;
1879 Op<1>() = V2;
1880 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001881 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001882}
1883
1884ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001885 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001886 BasicBlock *InsertAtEnd)
Dan Gohmane5af8cd2009-08-25 23:27:45 +00001887: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
1888 cast<VectorType>(Mask->getType())->getNumElements()),
1889 ShuffleVector,
1890 OperandTraits<ShuffleVectorInst>::op_begin(this),
1891 OperandTraits<ShuffleVectorInst>::operands(this),
1892 InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001893 assert(isValidOperands(V1, V2, Mask) &&
1894 "Invalid shuffle vector instruction operands!");
1895
Gabor Greif2d3024d2008-05-26 21:33:52 +00001896 Op<0>() = V1;
1897 Op<1>() = V2;
1898 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001899 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001900}
1901
Mon P Wang25f01062008-11-10 04:46:22 +00001902bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001903 const Value *Mask) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001904 // V1 and V2 must be vectors of the same type.
Duncan Sands19d0b472010-02-16 11:11:14 +00001905 if (!V1->getType()->isVectorTy() || V1->getType() != V2->getType())
Chris Lattnerf724e342008-03-02 05:28:33 +00001906 return false;
1907
Chris Lattner1dcb6542012-01-25 23:49:49 +00001908 // Mask must be vector of i32.
Sanjay Patel8bd52282017-04-19 16:22:19 +00001909 auto *MaskTy = dyn_cast<VectorType>(Mask->getType());
Craig Topperc6207612014-04-09 06:08:46 +00001910 if (!MaskTy || !MaskTy->getElementType()->isIntegerTy(32))
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001911 return false;
Nate Begeman60a31c32010-08-13 00:16:46 +00001912
1913 // Check to see if Mask is valid.
Chris Lattner1dcb6542012-01-25 23:49:49 +00001914 if (isa<UndefValue>(Mask) || isa<ConstantAggregateZero>(Mask))
1915 return true;
1916
Sanjay Patel8bd52282017-04-19 16:22:19 +00001917 if (const auto *MV = dyn_cast<ConstantVector>(Mask)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001918 unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements();
Benjamin Kramer3ad5c962014-03-10 15:03:06 +00001919 for (Value *Op : MV->operands()) {
Sanjay Patel8bd52282017-04-19 16:22:19 +00001920 if (auto *CI = dyn_cast<ConstantInt>(Op)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001921 if (CI->uge(V1Size*2))
Nate Begeman60a31c32010-08-13 00:16:46 +00001922 return false;
Benjamin Kramer3ad5c962014-03-10 15:03:06 +00001923 } else if (!isa<UndefValue>(Op)) {
Nate Begeman60a31c32010-08-13 00:16:46 +00001924 return false;
1925 }
1926 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001927 return true;
Mon P Wang6ebf4012011-10-26 00:34:48 +00001928 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001929
Sanjay Patel8bd52282017-04-19 16:22:19 +00001930 if (const auto *CDS = dyn_cast<ConstantDataSequential>(Mask)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001931 unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements();
1932 for (unsigned i = 0, e = MaskTy->getNumElements(); i != e; ++i)
1933 if (CDS->getElementAsInteger(i) >= V1Size*2)
1934 return false;
1935 return true;
1936 }
1937
1938 // The bitcode reader can create a place holder for a forward reference
1939 // used as the shuffle mask. When this occurs, the shuffle mask will
1940 // fall into this case and fail. To avoid this error, do this bit of
1941 // ugliness to allow such a mask pass.
Sanjay Patel8bd52282017-04-19 16:22:19 +00001942 if (const auto *CE = dyn_cast<ConstantExpr>(Mask))
Chris Lattner1dcb6542012-01-25 23:49:49 +00001943 if (CE->getOpcode() == Instruction::UserOp1)
1944 return true;
1945
1946 return false;
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001947}
1948
Chris Lattnercf129702012-01-26 02:51:13 +00001949int ShuffleVectorInst::getMaskValue(Constant *Mask, unsigned i) {
1950 assert(i < Mask->getType()->getVectorNumElements() && "Index out of range");
Sanjay Patel8bd52282017-04-19 16:22:19 +00001951 if (auto *CDS = dyn_cast<ConstantDataSequential>(Mask))
Chris Lattner1dcb6542012-01-25 23:49:49 +00001952 return CDS->getElementAsInteger(i);
Chris Lattnercf129702012-01-26 02:51:13 +00001953 Constant *C = Mask->getAggregateElement(i);
Chris Lattner1dcb6542012-01-25 23:49:49 +00001954 if (isa<UndefValue>(C))
Chris Lattnerf724e342008-03-02 05:28:33 +00001955 return -1;
Chris Lattner1dcb6542012-01-25 23:49:49 +00001956 return cast<ConstantInt>(C)->getZExtValue();
Chris Lattnerf724e342008-03-02 05:28:33 +00001957}
1958
Chris Lattnercf129702012-01-26 02:51:13 +00001959void ShuffleVectorInst::getShuffleMask(Constant *Mask,
1960 SmallVectorImpl<int> &Result) {
1961 unsigned NumElts = Mask->getType()->getVectorNumElements();
Chris Lattner1dcb6542012-01-25 23:49:49 +00001962
Sanjay Patel8bd52282017-04-19 16:22:19 +00001963 if (auto *CDS = dyn_cast<ConstantDataSequential>(Mask)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001964 for (unsigned i = 0; i != NumElts; ++i)
1965 Result.push_back(CDS->getElementAsInteger(i));
1966 return;
1967 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001968 for (unsigned i = 0; i != NumElts; ++i) {
1969 Constant *C = Mask->getAggregateElement(i);
1970 Result.push_back(isa<UndefValue>(C) ? -1 :
Chris Lattner3dbad4032012-01-26 00:41:50 +00001971 cast<ConstantInt>(C)->getZExtValue());
Chris Lattner1dcb6542012-01-25 23:49:49 +00001972 }
1973}
1974
1975
Dan Gohman12fce772008-05-15 19:50:34 +00001976//===----------------------------------------------------------------------===//
Dan Gohman0752bff2008-05-23 00:36:11 +00001977// InsertValueInst Class
1978//===----------------------------------------------------------------------===//
1979
Jay Foad57aa6362011-07-13 10:26:04 +00001980void InsertValueInst::init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
1981 const Twine &Name) {
Pete Cooperb4eede22015-06-12 17:48:10 +00001982 assert(getNumOperands() == 2 && "NumOperands not initialized?");
Jay Foad57aa6362011-07-13 10:26:04 +00001983
1984 // There's no fundamental reason why we require at least one index
1985 // (other than weirdness with &*IdxBegin being invalid; see
1986 // getelementptr's init routine for example). But there's no
1987 // present need to support it.
1988 assert(Idxs.size() > 0 && "InsertValueInst must have at least one index");
1989
1990 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs) ==
Frits van Bommel16ebe772010-12-05 20:50:26 +00001991 Val->getType() && "Inserted value must match indexed type!");
Dan Gohman1ecaf452008-05-31 00:58:22 +00001992 Op<0>() = Agg;
1993 Op<1>() = Val;
Dan Gohman0752bff2008-05-23 00:36:11 +00001994
Jay Foad57aa6362011-07-13 10:26:04 +00001995 Indices.append(Idxs.begin(), Idxs.end());
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001996 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001997}
1998
1999InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
Gabor Greife9408e62008-05-27 11:03:29 +00002000 : Instruction(IVI.getType(), InsertValue,
Dan Gohman1ecaf452008-05-31 00:58:22 +00002001 OperandTraits<InsertValueInst>::op_begin(this), 2),
2002 Indices(IVI.Indices) {
Dan Gohmand8ca05f2008-06-17 23:25:49 +00002003 Op<0>() = IVI.getOperand(0);
2004 Op<1>() = IVI.getOperand(1);
Dan Gohmanc8a27f22009-08-25 22:11:20 +00002005 SubclassOptionalData = IVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00002006}
2007
2008//===----------------------------------------------------------------------===//
Dan Gohman12fce772008-05-15 19:50:34 +00002009// ExtractValueInst Class
2010//===----------------------------------------------------------------------===//
2011
Jay Foad57aa6362011-07-13 10:26:04 +00002012void ExtractValueInst::init(ArrayRef<unsigned> Idxs, const Twine &Name) {
Pete Cooperb4eede22015-06-12 17:48:10 +00002013 assert(getNumOperands() == 1 && "NumOperands not initialized?");
Dan Gohman0752bff2008-05-23 00:36:11 +00002014
Jay Foad57aa6362011-07-13 10:26:04 +00002015 // There's no fundamental reason why we require at least one index.
2016 // But there's no present need to support it.
2017 assert(Idxs.size() > 0 && "ExtractValueInst must have at least one index");
Dan Gohman0752bff2008-05-23 00:36:11 +00002018
Jay Foad57aa6362011-07-13 10:26:04 +00002019 Indices.append(Idxs.begin(), Idxs.end());
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00002020 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00002021}
2022
2023ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
Gabor Greif21ba1842008-06-06 20:28:12 +00002024 : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)),
Dan Gohman1ecaf452008-05-31 00:58:22 +00002025 Indices(EVI.Indices) {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00002026 SubclassOptionalData = EVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00002027}
2028
Dan Gohman12fce772008-05-15 19:50:34 +00002029// getIndexedType - Returns the type of the element that would be extracted
2030// with an extractvalue instruction with the specified parameters.
2031//
2032// A null type is returned if the indices are invalid for the specified
2033// pointer type.
2034//
Chris Lattner229907c2011-07-18 04:54:35 +00002035Type *ExtractValueInst::getIndexedType(Type *Agg,
Jay Foad57aa6362011-07-13 10:26:04 +00002036 ArrayRef<unsigned> Idxs) {
Benjamin Kramer3ad5c962014-03-10 15:03:06 +00002037 for (unsigned Index : Idxs) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00002038 // We can't use CompositeType::indexValid(Index) here.
2039 // indexValid() always returns true for arrays because getelementptr allows
2040 // out-of-bounds indices. Since we don't allow those for extractvalue and
2041 // insertvalue we need to check array indexing manually.
2042 // Since the only other types we can index into are struct types it's just
2043 // as easy to check those manually as well.
Chris Lattner229907c2011-07-18 04:54:35 +00002044 if (ArrayType *AT = dyn_cast<ArrayType>(Agg)) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00002045 if (Index >= AT->getNumElements())
Craig Topperc6207612014-04-09 06:08:46 +00002046 return nullptr;
Chris Lattner229907c2011-07-18 04:54:35 +00002047 } else if (StructType *ST = dyn_cast<StructType>(Agg)) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00002048 if (Index >= ST->getNumElements())
Craig Topperc6207612014-04-09 06:08:46 +00002049 return nullptr;
Frits van Bommel16ebe772010-12-05 20:50:26 +00002050 } else {
2051 // Not a valid type to index into.
Craig Topperc6207612014-04-09 06:08:46 +00002052 return nullptr;
Frits van Bommel16ebe772010-12-05 20:50:26 +00002053 }
2054
2055 Agg = cast<CompositeType>(Agg)->getTypeAtIndex(Index);
Dan Gohman12fce772008-05-15 19:50:34 +00002056 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002057 return const_cast<Type*>(Agg);
Dan Gohman12fce772008-05-15 19:50:34 +00002058}
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002059
2060//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002061// BinaryOperator Class
2062//===----------------------------------------------------------------------===//
2063
Chris Lattner2195fc42007-02-24 00:55:48 +00002064BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Chris Lattner229907c2011-07-18 04:54:35 +00002065 Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00002066 Instruction *InsertBefore)
Dan Gohmana2414ea2010-05-03 22:44:19 +00002067 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00002068 OperandTraits<BinaryOperator>::op_begin(this),
2069 OperandTraits<BinaryOperator>::operands(this),
2070 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002071 Op<0>() = S1;
2072 Op<1>() = S2;
Dan Gohmana2414ea2010-05-03 22:44:19 +00002073 init(iType);
Chris Lattner2195fc42007-02-24 00:55:48 +00002074 setName(Name);
2075}
2076
2077BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Chris Lattner229907c2011-07-18 04:54:35 +00002078 Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00002079 BasicBlock *InsertAtEnd)
Dan Gohmana2414ea2010-05-03 22:44:19 +00002080 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00002081 OperandTraits<BinaryOperator>::op_begin(this),
2082 OperandTraits<BinaryOperator>::operands(this),
2083 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002084 Op<0>() = S1;
2085 Op<1>() = S2;
Dan Gohmana2414ea2010-05-03 22:44:19 +00002086 init(iType);
Chris Lattner2195fc42007-02-24 00:55:48 +00002087 setName(Name);
2088}
2089
2090
2091void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002092 Value *LHS = getOperand(0), *RHS = getOperand(1);
Jeffrey Yasskin9b43f332010-12-23 00:58:24 +00002093 (void)LHS; (void)RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002094 assert(LHS->getType() == RHS->getType() &&
2095 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002096#ifndef NDEBUG
2097 switch (iType) {
2098 case Add: case Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00002099 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002100 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002101 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002102 assert(getType()->isIntOrIntVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00002103 "Tried to create an integer operation on a non-integer type!");
2104 break;
2105 case FAdd: case FSub:
2106 case FMul:
2107 assert(getType() == LHS->getType() &&
2108 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002109 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00002110 "Tried to create a floating-point operation on a "
2111 "non-floating-point type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002112 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002113 case UDiv:
2114 case SDiv:
2115 assert(getType() == LHS->getType() &&
2116 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00002117 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00002118 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002119 "Incorrect operand type (not integer) for S/UDIV");
2120 break;
2121 case FDiv:
2122 assert(getType() == LHS->getType() &&
2123 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002124 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002125 "Incorrect operand type (not floating point) for FDIV");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002126 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00002127 case URem:
2128 case SRem:
2129 assert(getType() == LHS->getType() &&
2130 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00002131 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00002132 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00002133 "Incorrect operand type (not integer) for S/UREM");
2134 break;
2135 case FRem:
2136 assert(getType() == LHS->getType() &&
2137 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002138 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002139 "Incorrect operand type (not floating point) for FREM");
Reid Spencer7eb55b32006-11-02 01:53:59 +00002140 break;
Reid Spencer2341c222007-02-02 02:16:23 +00002141 case Shl:
2142 case LShr:
2143 case AShr:
2144 assert(getType() == LHS->getType() &&
2145 "Shift operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002146 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00002147 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00002148 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Nate Begemanfecbc8c2008-07-29 15:49:41 +00002149 "Tried to create a shift operation on a non-integral type!");
Reid Spencer2341c222007-02-02 02:16:23 +00002150 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002151 case And: case Or:
2152 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002153 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002154 "Logical operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002155 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00002156 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00002157 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00002158 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002159 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002160 default:
2161 break;
2162 }
2163#endif
2164}
2165
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002166BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002167 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002168 Instruction *InsertBefore) {
2169 assert(S1->getType() == S2->getType() &&
2170 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00002171 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002172}
2173
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002174BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002175 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002176 BasicBlock *InsertAtEnd) {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002177 BinaryOperator *Res = Create(Op, S1, S2, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002178 InsertAtEnd->getInstList().push_back(Res);
2179 return Res;
2180}
2181
Dan Gohman5476cfd2009-08-12 16:23:25 +00002182BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002183 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002184 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00002185 return new BinaryOperator(Instruction::Sub,
2186 zero, Op,
2187 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002188}
2189
Dan Gohman5476cfd2009-08-12 16:23:25 +00002190BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002191 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002192 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00002193 return new BinaryOperator(Instruction::Sub,
2194 zero, Op,
2195 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002196}
2197
Dan Gohman4ab44202009-12-18 02:58:50 +00002198BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
2199 Instruction *InsertBefore) {
2200 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2201 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertBefore);
2202}
2203
2204BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
2205 BasicBlock *InsertAtEnd) {
2206 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2207 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertAtEnd);
2208}
2209
Duncan Sandsfa5f5962010-02-02 12:53:04 +00002210BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
2211 Instruction *InsertBefore) {
2212 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2213 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertBefore);
2214}
2215
2216BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
2217 BasicBlock *InsertAtEnd) {
2218 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2219 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertAtEnd);
2220}
2221
Dan Gohman5476cfd2009-08-12 16:23:25 +00002222BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00002223 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002224 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Chris Lattner47a86bd2012-01-25 06:02:56 +00002225 return new BinaryOperator(Instruction::FSub, zero, Op,
Dan Gohmana5b96452009-06-04 22:49:04 +00002226 Op->getType(), Name, InsertBefore);
2227}
2228
Dan Gohman5476cfd2009-08-12 16:23:25 +00002229BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00002230 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002231 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Chris Lattner47a86bd2012-01-25 06:02:56 +00002232 return new BinaryOperator(Instruction::FSub, zero, Op,
Dan Gohmana5b96452009-06-04 22:49:04 +00002233 Op->getType(), Name, InsertAtEnd);
2234}
2235
Dan Gohman5476cfd2009-08-12 16:23:25 +00002236BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002237 Instruction *InsertBefore) {
Chris Lattner47a86bd2012-01-25 06:02:56 +00002238 Constant *C = Constant::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00002239 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002240 Op->getType(), Name, InsertBefore);
2241}
2242
Dan Gohman5476cfd2009-08-12 16:23:25 +00002243BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002244 BasicBlock *InsertAtEnd) {
Chris Lattner47a86bd2012-01-25 06:02:56 +00002245 Constant *AllOnes = Constant::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00002246 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002247 Op->getType(), Name, InsertAtEnd);
2248}
2249
2250
2251// isConstantAllOnes - Helper function for several functions below
2252static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner0256be92012-01-27 03:08:05 +00002253 if (const Constant *C = dyn_cast<Constant>(V))
2254 return C->isAllOnesValue();
Chris Lattner1edec382007-06-15 06:04:24 +00002255 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002256}
2257
Owen Andersonbb2501b2009-07-13 22:18:28 +00002258bool BinaryOperator::isNeg(const Value *V) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002259 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
2260 if (Bop->getOpcode() == Instruction::Sub)
Ana Pazosb3596022016-02-03 21:34:39 +00002261 if (Constant *C = dyn_cast<Constant>(Bop->getOperand(0)))
Owen Andersonbb2501b2009-07-13 22:18:28 +00002262 return C->isNegativeZeroValue();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002263 return false;
2264}
2265
Shuxin Yangf0537ab2013-01-09 00:13:41 +00002266bool BinaryOperator::isFNeg(const Value *V, bool IgnoreZeroSign) {
Dan Gohmana5b96452009-06-04 22:49:04 +00002267 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
2268 if (Bop->getOpcode() == Instruction::FSub)
Ana Pazosb3596022016-02-03 21:34:39 +00002269 if (Constant *C = dyn_cast<Constant>(Bop->getOperand(0))) {
Shuxin Yangf0537ab2013-01-09 00:13:41 +00002270 if (!IgnoreZeroSign)
2271 IgnoreZeroSign = cast<Instruction>(V)->hasNoSignedZeros();
2272 return !IgnoreZeroSign ? C->isNegativeZeroValue() : C->isZeroValue();
2273 }
Dan Gohmana5b96452009-06-04 22:49:04 +00002274 return false;
2275}
2276
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002277bool BinaryOperator::isNot(const Value *V) {
2278 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
2279 return (Bop->getOpcode() == Instruction::Xor &&
2280 (isConstantAllOnes(Bop->getOperand(1)) ||
2281 isConstantAllOnes(Bop->getOperand(0))));
2282 return false;
2283}
2284
Chris Lattner2c7d1772005-04-24 07:28:37 +00002285Value *BinaryOperator::getNegArgument(Value *BinOp) {
Chris Lattner2c7d1772005-04-24 07:28:37 +00002286 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002287}
2288
Chris Lattner2c7d1772005-04-24 07:28:37 +00002289const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
2290 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002291}
2292
Dan Gohmana5b96452009-06-04 22:49:04 +00002293Value *BinaryOperator::getFNegArgument(Value *BinOp) {
Dan Gohmana5b96452009-06-04 22:49:04 +00002294 return cast<BinaryOperator>(BinOp)->getOperand(1);
2295}
2296
2297const Value *BinaryOperator::getFNegArgument(const Value *BinOp) {
2298 return getFNegArgument(const_cast<Value*>(BinOp));
2299}
2300
Chris Lattner2c7d1772005-04-24 07:28:37 +00002301Value *BinaryOperator::getNotArgument(Value *BinOp) {
2302 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
2303 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
2304 Value *Op0 = BO->getOperand(0);
2305 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002306 if (isConstantAllOnes(Op0)) return Op1;
2307
2308 assert(isConstantAllOnes(Op1));
2309 return Op0;
2310}
2311
Chris Lattner2c7d1772005-04-24 07:28:37 +00002312const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
2313 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002314}
2315
2316
Sanjay Patel4ce99d42016-11-16 18:09:44 +00002317// Exchange the two operands to this instruction. This instruction is safe to
2318// use on any binary instruction and does not modify the semantics of the
2319// instruction. If the instruction is order-dependent (SetLT f.e.), the opcode
2320// is changed.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002321bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00002322 if (!isCommutative())
2323 return true; // Can't commute operands
Gabor Greif5ef74042008-05-13 22:51:52 +00002324 Op<0>().swap(Op<1>());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002325 return false;
2326}
2327
Sanjay Patela982d992014-09-03 01:06:50 +00002328
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002329//===----------------------------------------------------------------------===//
Duncan Sands05f4df82012-04-16 16:28:59 +00002330// FPMathOperator Class
2331//===----------------------------------------------------------------------===//
2332
Duncan Sands05f4df82012-04-16 16:28:59 +00002333float FPMathOperator::getFPAccuracy() const {
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +00002334 const MDNode *MD =
2335 cast<Instruction>(this)->getMetadata(LLVMContext::MD_fpmath);
Duncan Sands05f4df82012-04-16 16:28:59 +00002336 if (!MD)
2337 return 0.0;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002338 ConstantFP *Accuracy = mdconst::extract<ConstantFP>(MD->getOperand(0));
Duncan Sands9af62982012-04-16 19:39:33 +00002339 return Accuracy->getValueAPF().convertToFloat();
Duncan Sands05f4df82012-04-16 16:28:59 +00002340}
2341
2342
2343//===----------------------------------------------------------------------===//
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002344// CastInst Class
2345//===----------------------------------------------------------------------===//
2346
David Blaikie3a15e142011-12-01 08:00:17 +00002347void CastInst::anchor() {}
2348
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002349// Just determine if this cast only deals with integral->integral conversion.
2350bool CastInst::isIntegerCast() const {
2351 switch (getOpcode()) {
2352 default: return false;
2353 case Instruction::ZExt:
2354 case Instruction::SExt:
2355 case Instruction::Trunc:
2356 return true;
2357 case Instruction::BitCast:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002358 return getOperand(0)->getType()->isIntegerTy() &&
2359 getType()->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002360 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002361}
2362
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002363bool CastInst::isLosslessCast() const {
2364 // Only BitCast can be lossless, exit fast if we're not BitCast
2365 if (getOpcode() != Instruction::BitCast)
2366 return false;
2367
2368 // Identity cast is always lossless
Ana Pazosb3596022016-02-03 21:34:39 +00002369 Type *SrcTy = getOperand(0)->getType();
2370 Type *DstTy = getType();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002371 if (SrcTy == DstTy)
2372 return true;
2373
Reid Spencer8d9336d2006-12-31 05:26:44 +00002374 // Pointer to pointer is always lossless.
Duncan Sands19d0b472010-02-16 11:11:14 +00002375 if (SrcTy->isPointerTy())
2376 return DstTy->isPointerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002377 return false; // Other types have no identity values
2378}
2379
2380/// This function determines if the CastInst does not require any bits to be
2381/// changed in order to effect the cast. Essentially, it identifies cases where
2382/// no code gen is necessary for the cast, hence the name no-op cast. For
2383/// example, the following are all no-op casts:
Dan Gohmane9bc2ba2008-05-12 16:34:30 +00002384/// # bitcast i32* %x to i8*
2385/// # bitcast <2 x i32> %x to <4 x i16>
2386/// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002387/// @brief Determine if the described cast is a no-op.
2388bool CastInst::isNoopCast(Instruction::CastOps Opcode,
Chris Lattner229907c2011-07-18 04:54:35 +00002389 Type *SrcTy,
2390 Type *DestTy,
2391 Type *IntPtrTy) {
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002392 switch (Opcode) {
Craig Topperc514b542012-02-05 22:14:15 +00002393 default: llvm_unreachable("Invalid CastOp");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002394 case Instruction::Trunc:
2395 case Instruction::ZExt:
2396 case Instruction::SExt:
2397 case Instruction::FPTrunc:
2398 case Instruction::FPExt:
2399 case Instruction::UIToFP:
2400 case Instruction::SIToFP:
2401 case Instruction::FPToUI:
2402 case Instruction::FPToSI:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002403 case Instruction::AddrSpaceCast:
2404 // TODO: Target informations may give a more accurate answer here.
2405 return false;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002406 case Instruction::BitCast:
2407 return true; // BitCast never modifies bits.
2408 case Instruction::PtrToInt:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002409 return IntPtrTy->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002410 DestTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002411 case Instruction::IntToPtr:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002412 return IntPtrTy->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002413 SrcTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002414 }
2415}
2416
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002417/// @brief Determine if a cast is a no-op.
Chris Lattner229907c2011-07-18 04:54:35 +00002418bool CastInst::isNoopCast(Type *IntPtrTy) const {
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002419 return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), IntPtrTy);
2420}
2421
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002422bool CastInst::isNoopCast(const DataLayout &DL) const {
Craig Topperc6207612014-04-09 06:08:46 +00002423 Type *PtrOpTy = nullptr;
Matt Arsenaulta236ea52014-03-06 17:33:55 +00002424 if (getOpcode() == Instruction::PtrToInt)
2425 PtrOpTy = getOperand(0)->getType();
2426 else if (getOpcode() == Instruction::IntToPtr)
2427 PtrOpTy = getType();
2428
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002429 Type *IntPtrTy =
2430 PtrOpTy ? DL.getIntPtrType(PtrOpTy) : DL.getIntPtrType(getContext(), 0);
Matt Arsenaulta236ea52014-03-06 17:33:55 +00002431
2432 return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), IntPtrTy);
2433}
2434
2435/// This function determines if a pair of casts can be eliminated and what
2436/// opcode should be used in the elimination. This assumes that there are two
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002437/// instructions like this:
2438/// * %F = firstOpcode SrcTy %x to MidTy
2439/// * %S = secondOpcode MidTy %F to DstTy
2440/// The function returns a resultOpcode so these two casts can be replaced with:
2441/// * %Replacement = resultOpcode %SrcTy %x to DstTy
Sanjay Patel4dad27e2015-12-11 18:12:01 +00002442/// If no such cast is permitted, the function returns 0.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002443unsigned CastInst::isEliminableCastPair(
2444 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
Duncan Sandse2395dc2012-10-30 16:03:32 +00002445 Type *SrcTy, Type *MidTy, Type *DstTy, Type *SrcIntPtrTy, Type *MidIntPtrTy,
2446 Type *DstIntPtrTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002447 // Define the 144 possibilities for these two cast instructions. The values
2448 // in this matrix determine what to do in a given situation and select the
2449 // case in the switch below. The rows correspond to firstOp, the columns
Sanjay Patel4dad27e2015-12-11 18:12:01 +00002450 // correspond to secondOp. In looking at the table below, keep in mind
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002451 // the following cast properties:
2452 //
2453 // Size Compare Source Destination
2454 // Operator Src ? Size Type Sign Type Sign
2455 // -------- ------------ ------------------- ---------------------
2456 // TRUNC > Integer Any Integral Any
2457 // ZEXT < Integral Unsigned Integer Any
2458 // SEXT < Integral Signed Integer Any
2459 // FPTOUI n/a FloatPt n/a Integral Unsigned
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002460 // FPTOSI n/a FloatPt n/a Integral Signed
2461 // UITOFP n/a Integral Unsigned FloatPt n/a
2462 // SITOFP n/a Integral Signed FloatPt n/a
2463 // FPTRUNC > FloatPt n/a FloatPt n/a
2464 // FPEXT < FloatPt n/a FloatPt n/a
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002465 // PTRTOINT n/a Pointer n/a Integral Unsigned
2466 // INTTOPTR n/a Integral Unsigned Pointer n/a
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002467 // BITCAST = FirstClass n/a FirstClass n/a
2468 // ADDRSPCST n/a Pointer n/a Pointer n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00002469 //
2470 // NOTE: some transforms are safe, but we consider them to be non-profitable.
Dan Gohman4fe64de2009-06-14 23:30:43 +00002471 // For example, we could merge "fptoui double to i32" + "zext i32 to i64",
2472 // into "fptoui double to i64", but this loses information about the range
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002473 // of the produced value (we no longer know the top-part is all zeros).
Chris Lattner6f6b4972006-12-05 23:43:59 +00002474 // Further this conversion is often much more expensive for typical hardware,
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002475 // and causes issues when building libgcc. We disallow fptosi+sext for the
Chris Lattner6f6b4972006-12-05 23:43:59 +00002476 // same reason.
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002477 const unsigned numCastOps =
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002478 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
2479 static const uint8_t CastResults[numCastOps][numCastOps] = {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002480 // T F F U S F F P I B A -+
2481 // R Z S P P I I T P 2 N T S |
2482 // U E E 2 2 2 2 R E I T C C +- secondOp
2483 // N X X U S F F N X N 2 V V |
2484 // C T T I I P P C T T P T T -+
2485 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // Trunc -+
Fiona Glaser0d41db12015-04-21 00:05:41 +00002486 { 8, 1, 9,99,99, 2,17,99,99,99, 2, 3, 0}, // ZExt |
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002487 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3, 0}, // SExt |
2488 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // FPToUI |
2489 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // FPToSI |
2490 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // UIToFP +- firstOp
2491 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // SIToFP |
Ahmed Bougacha0ea9d1e2015-05-29 00:04:30 +00002492 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // FPTrunc |
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002493 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4, 0}, // FPExt |
2494 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3, 0}, // PtrToInt |
2495 { 99,99,99,99,99,99,99,99,99,11,99,15, 0}, // IntToPtr |
2496 { 5, 5, 5, 6, 6, 5, 5, 6, 6,16, 5, 1,14}, // BitCast |
2497 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,13,12}, // AddrSpaceCast -+
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002498 };
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002499
Sanjay Patel93f55dd2015-12-12 00:33:36 +00002500 // TODO: This logic could be encoded into the table above and handled in the
2501 // switch below.
Chris Lattner25eea4d2010-07-12 01:19:22 +00002502 // If either of the casts are a bitcast from scalar to vector, disallow the
Sanjay Patel93f55dd2015-12-12 00:33:36 +00002503 // merging. However, any pair of bitcasts are allowed.
2504 bool IsFirstBitcast = (firstOp == Instruction::BitCast);
2505 bool IsSecondBitcast = (secondOp == Instruction::BitCast);
2506 bool AreBothBitcasts = IsFirstBitcast && IsSecondBitcast;
Nadav Rotem5fc81ff2011-08-29 19:58:36 +00002507
Sanjay Patel93f55dd2015-12-12 00:33:36 +00002508 // Check if any of the casts convert scalars <-> vectors.
2509 if ((IsFirstBitcast && isa<VectorType>(SrcTy) != isa<VectorType>(MidTy)) ||
2510 (IsSecondBitcast && isa<VectorType>(MidTy) != isa<VectorType>(DstTy)))
2511 if (!AreBothBitcasts)
2512 return 0;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002513
2514 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
2515 [secondOp-Instruction::CastOpsBegin];
2516 switch (ElimCase) {
2517 case 0:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002518 // Categorically disallowed.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002519 return 0;
2520 case 1:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002521 // Allowed, use first cast's opcode.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002522 return firstOp;
2523 case 2:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002524 // Allowed, use second cast's opcode.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002525 return secondOp;
2526 case 3:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002527 // No-op cast in second op implies firstOp as long as the DestTy
Mon P Wange04b4562010-01-23 04:35:57 +00002528 // is integer and we are not converting between a vector and a
Alp Tokerf907b892013-12-05 05:44:44 +00002529 // non-vector type.
Duncan Sands19d0b472010-02-16 11:11:14 +00002530 if (!SrcTy->isVectorTy() && DstTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002531 return firstOp;
2532 return 0;
2533 case 4:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002534 // No-op cast in second op implies firstOp as long as the DestTy
Chris Lattner531732b2010-01-23 04:42:42 +00002535 // is floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002536 if (DstTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002537 return firstOp;
2538 return 0;
2539 case 5:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002540 // No-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002541 // is an integer.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002542 if (SrcTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002543 return secondOp;
2544 return 0;
2545 case 6:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002546 // No-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002547 // is a floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002548 if (SrcTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002549 return secondOp;
2550 return 0;
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002551 case 7: {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002552 // Cannot simplify if address spaces are different!
2553 if (SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace())
2554 return 0;
2555
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002556 unsigned MidSize = MidTy->getScalarSizeInBits();
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002557 // We can still fold this without knowing the actual sizes as long we
2558 // know that the intermediate pointer is the largest possible
2559 // pointer size.
2560 // FIXME: Is this always true?
2561 if (MidSize == 64)
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002562 return Instruction::BitCast;
2563
2564 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size.
Duncan Sandse2395dc2012-10-30 16:03:32 +00002565 if (!SrcIntPtrTy || DstIntPtrTy != SrcIntPtrTy)
Dan Gohman9413de12009-07-21 23:19:40 +00002566 return 0;
Duncan Sandse2395dc2012-10-30 16:03:32 +00002567 unsigned PtrSize = SrcIntPtrTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002568 if (MidSize >= PtrSize)
2569 return Instruction::BitCast;
2570 return 0;
2571 }
2572 case 8: {
2573 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
2574 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
2575 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002576 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2577 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002578 if (SrcSize == DstSize)
2579 return Instruction::BitCast;
2580 else if (SrcSize < DstSize)
2581 return firstOp;
2582 return secondOp;
2583 }
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002584 case 9:
2585 // zext, sext -> zext, because sext can't sign extend after zext
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002586 return Instruction::ZExt;
2587 case 10:
2588 // fpext followed by ftrunc is allowed if the bit size returned to is
2589 // the same as the original, in which case its just a bitcast
2590 if (SrcTy == DstTy)
2591 return Instruction::BitCast;
2592 return 0; // If the types are not the same we can't eliminate it.
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002593 case 11: {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002594 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
Duncan Sandse2395dc2012-10-30 16:03:32 +00002595 if (!MidIntPtrTy)
Dan Gohman9413de12009-07-21 23:19:40 +00002596 return 0;
Duncan Sandse2395dc2012-10-30 16:03:32 +00002597 unsigned PtrSize = MidIntPtrTy->getScalarSizeInBits();
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002598 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2599 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002600 if (SrcSize <= PtrSize && SrcSize == DstSize)
2601 return Instruction::BitCast;
2602 return 0;
2603 }
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002604 case 12: {
2605 // addrspacecast, addrspacecast -> bitcast, if SrcAS == DstAS
2606 // addrspacecast, addrspacecast -> addrspacecast, if SrcAS != DstAS
2607 if (SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace())
2608 return Instruction::AddrSpaceCast;
2609 return Instruction::BitCast;
2610 }
2611 case 13:
2612 // FIXME: this state can be merged with (1), but the following assert
2613 // is useful to check the correcteness of the sequence due to semantic
2614 // change of bitcast.
2615 assert(
2616 SrcTy->isPtrOrPtrVectorTy() &&
2617 MidTy->isPtrOrPtrVectorTy() &&
2618 DstTy->isPtrOrPtrVectorTy() &&
2619 SrcTy->getPointerAddressSpace() != MidTy->getPointerAddressSpace() &&
2620 MidTy->getPointerAddressSpace() == DstTy->getPointerAddressSpace() &&
2621 "Illegal addrspacecast, bitcast sequence!");
2622 // Allowed, use first cast's opcode
2623 return firstOp;
2624 case 14:
Jingyue Wu77145d92014-06-06 21:52:55 +00002625 // bitcast, addrspacecast -> addrspacecast if the element type of
2626 // bitcast's source is the same as that of addrspacecast's destination.
Peter Collingbourne9d5fd4d2016-11-13 06:58:45 +00002627 if (SrcTy->getScalarType()->getPointerElementType() ==
2628 DstTy->getScalarType()->getPointerElementType())
Jingyue Wu77145d92014-06-06 21:52:55 +00002629 return Instruction::AddrSpaceCast;
2630 return 0;
2631
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002632 case 15:
2633 // FIXME: this state can be merged with (1), but the following assert
2634 // is useful to check the correcteness of the sequence due to semantic
2635 // change of bitcast.
2636 assert(
2637 SrcTy->isIntOrIntVectorTy() &&
2638 MidTy->isPtrOrPtrVectorTy() &&
2639 DstTy->isPtrOrPtrVectorTy() &&
2640 MidTy->getPointerAddressSpace() == DstTy->getPointerAddressSpace() &&
2641 "Illegal inttoptr, bitcast sequence!");
2642 // Allowed, use first cast's opcode
2643 return firstOp;
2644 case 16:
2645 // FIXME: this state can be merged with (2), but the following assert
2646 // is useful to check the correcteness of the sequence due to semantic
2647 // change of bitcast.
2648 assert(
2649 SrcTy->isPtrOrPtrVectorTy() &&
2650 MidTy->isPtrOrPtrVectorTy() &&
2651 DstTy->isIntOrIntVectorTy() &&
2652 SrcTy->getPointerAddressSpace() == MidTy->getPointerAddressSpace() &&
2653 "Illegal bitcast, ptrtoint sequence!");
2654 // Allowed, use second cast's opcode
2655 return secondOp;
Fiona Glaser0d41db12015-04-21 00:05:41 +00002656 case 17:
2657 // (sitofp (zext x)) -> (uitofp x)
2658 return Instruction::UIToFP;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002659 case 99:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002660 // Cast combination can't happen (error in input). This is for all cases
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002661 // where the MidTy is not the same for the two cast instructions.
Craig Topperc514b542012-02-05 22:14:15 +00002662 llvm_unreachable("Invalid Cast Combination");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002663 default:
Craig Topperc514b542012-02-05 22:14:15 +00002664 llvm_unreachable("Error in CastResults table!!!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002665 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002666}
2667
Chris Lattner229907c2011-07-18 04:54:35 +00002668CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002669 const Twine &Name, Instruction *InsertBefore) {
Duncan Sands7f646562011-05-18 09:21:57 +00002670 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002671 // Construct and return the appropriate CastInst subclass
2672 switch (op) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002673 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
2674 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
2675 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
2676 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
2677 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
2678 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
2679 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
2680 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
2681 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
2682 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
2683 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
2684 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
2685 case AddrSpaceCast: return new AddrSpaceCastInst (S, Ty, Name, InsertBefore);
2686 default: llvm_unreachable("Invalid opcode provided");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002687 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002688}
2689
Chris Lattner229907c2011-07-18 04:54:35 +00002690CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002691 const Twine &Name, BasicBlock *InsertAtEnd) {
Duncan Sands7f646562011-05-18 09:21:57 +00002692 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002693 // Construct and return the appropriate CastInst subclass
2694 switch (op) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002695 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
2696 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
2697 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
2698 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
2699 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
2700 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
2701 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
2702 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
2703 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
2704 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
2705 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
2706 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
2707 case AddrSpaceCast: return new AddrSpaceCastInst (S, Ty, Name, InsertAtEnd);
2708 default: llvm_unreachable("Invalid opcode provided");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002709 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002710}
2711
Chris Lattner229907c2011-07-18 04:54:35 +00002712CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002713 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002714 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002715 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002716 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2717 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002718}
2719
Chris Lattner229907c2011-07-18 04:54:35 +00002720CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002721 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002722 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002723 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002724 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2725 return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002726}
2727
Chris Lattner229907c2011-07-18 04:54:35 +00002728CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002729 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002730 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002731 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002732 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2733 return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002734}
2735
Chris Lattner229907c2011-07-18 04:54:35 +00002736CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002737 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002738 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002739 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002740 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2741 return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002742}
2743
Chris Lattner229907c2011-07-18 04:54:35 +00002744CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002745 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002746 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002747 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002748 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2749 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002750}
2751
Chris Lattner229907c2011-07-18 04:54:35 +00002752CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002753 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002754 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002755 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002756 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2757 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002758}
2759
Chris Lattner229907c2011-07-18 04:54:35 +00002760CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002761 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002762 BasicBlock *InsertAtEnd) {
Matt Arsenault065ced92013-07-31 00:17:33 +00002763 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2764 assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) &&
2765 "Invalid cast");
2766 assert(Ty->isVectorTy() == S->getType()->isVectorTy() && "Invalid cast");
Richard Trieu8dc43232013-07-31 04:07:28 +00002767 assert((!Ty->isVectorTy() ||
2768 Ty->getVectorNumElements() == S->getType()->getVectorNumElements()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002769 "Invalid cast");
2770
Matt Arsenault065ced92013-07-31 00:17:33 +00002771 if (Ty->isIntOrIntVectorTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002772 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002773
Matt Arsenault740980e2014-07-14 17:24:35 +00002774 return CreatePointerBitCastOrAddrSpaceCast(S, Ty, Name, InsertAtEnd);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002775}
2776
2777/// @brief Create a BitCast or a PtrToInt cast instruction
Matt Arsenault065ced92013-07-31 00:17:33 +00002778CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
2779 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002780 Instruction *InsertBefore) {
Evgeniy Stepanovc9bd35b2013-01-15 16:43:00 +00002781 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2782 assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002783 "Invalid cast");
Matt Arsenault065ced92013-07-31 00:17:33 +00002784 assert(Ty->isVectorTy() == S->getType()->isVectorTy() && "Invalid cast");
Richard Trieu8dc43232013-07-31 04:07:28 +00002785 assert((!Ty->isVectorTy() ||
2786 Ty->getVectorNumElements() == S->getType()->getVectorNumElements()) &&
Matt Arsenault065ced92013-07-31 00:17:33 +00002787 "Invalid cast");
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002788
Evgeniy Stepanovc9bd35b2013-01-15 16:43:00 +00002789 if (Ty->isIntOrIntVectorTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002790 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002791
Matt Arsenault740980e2014-07-14 17:24:35 +00002792 return CreatePointerBitCastOrAddrSpaceCast(S, Ty, Name, InsertBefore);
2793}
2794
2795CastInst *CastInst::CreatePointerBitCastOrAddrSpaceCast(
2796 Value *S, Type *Ty,
2797 const Twine &Name,
2798 BasicBlock *InsertAtEnd) {
2799 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2800 assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast");
2801
2802 if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace())
2803 return Create(Instruction::AddrSpaceCast, S, Ty, Name, InsertAtEnd);
2804
2805 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2806}
2807
2808CastInst *CastInst::CreatePointerBitCastOrAddrSpaceCast(
2809 Value *S, Type *Ty,
2810 const Twine &Name,
2811 Instruction *InsertBefore) {
2812 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2813 assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast");
2814
2815 if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace())
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002816 return Create(Instruction::AddrSpaceCast, S, Ty, Name, InsertBefore);
2817
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002818 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002819}
2820
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002821CastInst *CastInst::CreateBitOrPointerCast(Value *S, Type *Ty,
2822 const Twine &Name,
2823 Instruction *InsertBefore) {
2824 if (S->getType()->isPointerTy() && Ty->isIntegerTy())
2825 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
2826 if (S->getType()->isIntegerTy() && Ty->isPointerTy())
2827 return Create(Instruction::IntToPtr, S, Ty, Name, InsertBefore);
2828
2829 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2830}
2831
Matt Arsenault740980e2014-07-14 17:24:35 +00002832CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002833 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002834 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002835 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Chris Lattner5370ae72010-01-10 20:21:42 +00002836 "Invalid integer cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002837 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2838 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002839 Instruction::CastOps opcode =
2840 (SrcBits == DstBits ? Instruction::BitCast :
2841 (SrcBits > DstBits ? Instruction::Trunc :
2842 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002843 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002844}
2845
Chris Lattner229907c2011-07-18 04:54:35 +00002846CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002847 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002848 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002849 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002850 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002851 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2852 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002853 Instruction::CastOps opcode =
2854 (SrcBits == DstBits ? Instruction::BitCast :
2855 (SrcBits > DstBits ? Instruction::Trunc :
2856 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002857 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002858}
2859
Chris Lattner229907c2011-07-18 04:54:35 +00002860CastInst *CastInst::CreateFPCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002861 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002862 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002863 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002864 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002865 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2866 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002867 Instruction::CastOps opcode =
2868 (SrcBits == DstBits ? Instruction::BitCast :
2869 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002870 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002871}
2872
Chris Lattner229907c2011-07-18 04:54:35 +00002873CastInst *CastInst::CreateFPCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002874 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002875 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002876 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002877 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002878 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2879 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002880 Instruction::CastOps opcode =
2881 (SrcBits == DstBits ? Instruction::BitCast :
2882 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002883 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002884}
2885
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002886// Check whether it is valid to call getCastOpcode for these types.
2887// This routine must be kept in sync with getCastOpcode.
2888bool CastInst::isCastable(Type *SrcTy, Type *DestTy) {
2889 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2890 return false;
2891
2892 if (SrcTy == DestTy)
2893 return true;
2894
2895 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
2896 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
2897 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2898 // An element by element cast. Valid if casting the elements is valid.
2899 SrcTy = SrcVecTy->getElementType();
2900 DestTy = DestVecTy->getElementType();
2901 }
2902
2903 // Get the bit sizes, we'll need these
2904 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2905 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
2906
2907 // Run through the possibilities ...
2908 if (DestTy->isIntegerTy()) { // Casting to integral
David Blaikie9965c5a2015-03-23 19:51:23 +00002909 if (SrcTy->isIntegerTy()) // Casting from integral
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002910 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002911 if (SrcTy->isFloatingPointTy()) // Casting from floating pt
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002912 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002913 if (SrcTy->isVectorTy()) // Casting from vector
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002914 return DestBits == SrcBits;
David Blaikie9965c5a2015-03-23 19:51:23 +00002915 // Casting from something else
2916 return SrcTy->isPointerTy();
2917 }
2918 if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2919 if (SrcTy->isIntegerTy()) // Casting from integral
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002920 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002921 if (SrcTy->isFloatingPointTy()) // Casting from floating pt
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002922 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002923 if (SrcTy->isVectorTy()) // Casting from vector
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002924 return DestBits == SrcBits;
David Blaikie9965c5a2015-03-23 19:51:23 +00002925 // Casting from something else
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002926 return false;
2927 }
David Blaikie9965c5a2015-03-23 19:51:23 +00002928 if (DestTy->isVectorTy()) // Casting to vector
2929 return DestBits == SrcBits;
2930 if (DestTy->isPointerTy()) { // Casting to pointer
2931 if (SrcTy->isPointerTy()) // Casting from pointer
2932 return true;
2933 return SrcTy->isIntegerTy(); // Casting from integral
2934 }
2935 if (DestTy->isX86_MMXTy()) {
2936 if (SrcTy->isVectorTy())
2937 return DestBits == SrcBits; // 64-bit vector to MMX
2938 return false;
2939 } // Casting to something else
2940 return false;
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002941}
2942
Matt Arsenaultcacbb232013-07-30 20:45:05 +00002943bool CastInst::isBitCastable(Type *SrcTy, Type *DestTy) {
2944 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2945 return false;
2946
2947 if (SrcTy == DestTy)
2948 return true;
2949
2950 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) {
2951 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy)) {
2952 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2953 // An element by element cast. Valid if casting the elements is valid.
2954 SrcTy = SrcVecTy->getElementType();
2955 DestTy = DestVecTy->getElementType();
2956 }
2957 }
2958 }
2959
2960 if (PointerType *DestPtrTy = dyn_cast<PointerType>(DestTy)) {
2961 if (PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy)) {
2962 return SrcPtrTy->getAddressSpace() == DestPtrTy->getAddressSpace();
2963 }
2964 }
2965
2966 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2967 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
2968
2969 // Could still have vectors of pointers if the number of elements doesn't
2970 // match
2971 if (SrcBits == 0 || DestBits == 0)
2972 return false;
2973
2974 if (SrcBits != DestBits)
2975 return false;
2976
2977 if (DestTy->isX86_MMXTy() || SrcTy->isX86_MMXTy())
2978 return false;
2979
2980 return true;
2981}
2982
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002983bool CastInst::isBitOrNoopPointerCastable(Type *SrcTy, Type *DestTy,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002984 const DataLayout &DL) {
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002985 if (auto *PtrTy = dyn_cast<PointerType>(SrcTy))
2986 if (auto *IntTy = dyn_cast<IntegerType>(DestTy))
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002987 return IntTy->getBitWidth() == DL.getPointerTypeSizeInBits(PtrTy);
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002988 if (auto *PtrTy = dyn_cast<PointerType>(DestTy))
2989 if (auto *IntTy = dyn_cast<IntegerType>(SrcTy))
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002990 return IntTy->getBitWidth() == DL.getPointerTypeSizeInBits(PtrTy);
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002991
2992 return isBitCastable(SrcTy, DestTy);
2993}
2994
Matt Arsenaultcacbb232013-07-30 20:45:05 +00002995// Provide a way to get a "cast" where the cast opcode is inferred from the
2996// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002997// logic in the castIsValid function below. This axiom should hold:
2998// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2999// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003000// casting opcode for the arguments passed to it.
Duncan Sands55e50902008-01-06 10:12:28 +00003001// This routine must be kept in sync with isCastable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003002Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00003003CastInst::getCastOpcode(
Chris Lattner229907c2011-07-18 04:54:35 +00003004 const Value *Src, bool SrcIsSigned, Type *DestTy, bool DestIsSigned) {
3005 Type *SrcTy = Src->getType();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003006
Duncan Sands55e50902008-01-06 10:12:28 +00003007 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
3008 "Only first class types are castable!");
3009
Duncan Sandsa8514532011-05-18 07:13:41 +00003010 if (SrcTy == DestTy)
3011 return BitCast;
3012
Matt Arsenaultcacbb232013-07-30 20:45:05 +00003013 // FIXME: Check address space sizes here
Chris Lattner229907c2011-07-18 04:54:35 +00003014 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
3015 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
Duncan Sandsa8514532011-05-18 07:13:41 +00003016 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
3017 // An element by element cast. Find the appropriate opcode based on the
3018 // element types.
3019 SrcTy = SrcVecTy->getElementType();
3020 DestTy = DestVecTy->getElementType();
3021 }
3022
3023 // Get the bit sizes, we'll need these
Duncan Sands7f646562011-05-18 09:21:57 +00003024 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
3025 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
Duncan Sandsa8514532011-05-18 07:13:41 +00003026
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003027 // Run through the possibilities ...
Duncan Sands9dff9be2010-02-15 16:12:20 +00003028 if (DestTy->isIntegerTy()) { // Casting to integral
3029 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003030 if (DestBits < SrcBits)
3031 return Trunc; // int -> smaller int
3032 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00003033 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003034 return SExt; // signed -> SEXT
3035 else
3036 return ZExt; // unsigned -> ZEXT
3037 } else {
3038 return BitCast; // Same size, No-op cast
3039 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00003040 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00003041 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003042 return FPToSI; // FP -> sint
3043 else
3044 return FPToUI; // FP -> uint
Duncan Sands27bd0df2011-05-18 10:59:25 +00003045 } else if (SrcTy->isVectorTy()) {
3046 assert(DestBits == SrcBits &&
3047 "Casting vector to integer of different width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003048 return BitCast; // Same size, no-op cast
3049 } else {
Duncan Sands19d0b472010-02-16 11:11:14 +00003050 assert(SrcTy->isPointerTy() &&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003051 "Casting from a value that is not first-class type");
3052 return PtrToInt; // ptr -> int
3053 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00003054 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
3055 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00003056 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003057 return SIToFP; // sint -> FP
3058 else
3059 return UIToFP; // uint -> FP
Duncan Sands9dff9be2010-02-15 16:12:20 +00003060 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003061 if (DestBits < SrcBits) {
3062 return FPTrunc; // FP -> smaller FP
3063 } else if (DestBits > SrcBits) {
3064 return FPExt; // FP -> larger FP
3065 } else {
3066 return BitCast; // same size, no-op cast
3067 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00003068 } else if (SrcTy->isVectorTy()) {
3069 assert(DestBits == SrcBits &&
Dan Gohmanfead7972007-05-11 21:43:24 +00003070 "Casting vector to floating point of different width");
Devang Patele9432132008-11-05 01:37:40 +00003071 return BitCast; // same size, no-op cast
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003072 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00003073 llvm_unreachable("Casting pointer or non-first class to float");
Duncan Sands27bd0df2011-05-18 10:59:25 +00003074 } else if (DestTy->isVectorTy()) {
3075 assert(DestBits == SrcBits &&
3076 "Illegal cast to vector (wrong type or size)");
3077 return BitCast;
Duncan Sands19d0b472010-02-16 11:11:14 +00003078 } else if (DestTy->isPointerTy()) {
3079 if (SrcTy->isPointerTy()) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003080 if (DestTy->getPointerAddressSpace() != SrcTy->getPointerAddressSpace())
3081 return AddrSpaceCast;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003082 return BitCast; // ptr -> ptr
Duncan Sands9dff9be2010-02-15 16:12:20 +00003083 } else if (SrcTy->isIntegerTy()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003084 return IntToPtr; // int -> ptr
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003085 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00003086 llvm_unreachable("Casting pointer to other than pointer or int");
Dale Johannesendd224d22010-09-30 23:57:10 +00003087 } else if (DestTy->isX86_MMXTy()) {
Duncan Sands27bd0df2011-05-18 10:59:25 +00003088 if (SrcTy->isVectorTy()) {
3089 assert(DestBits == SrcBits && "Casting vector of wrong width to X86_MMX");
Dale Johannesendd224d22010-09-30 23:57:10 +00003090 return BitCast; // 64-bit vector to MMX
Dale Johannesendd224d22010-09-30 23:57:10 +00003091 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00003092 llvm_unreachable("Illegal cast to X86_MMX");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003093 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00003094 llvm_unreachable("Casting to type that is not first-class");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003095}
3096
3097//===----------------------------------------------------------------------===//
3098// CastInst SubClass Constructors
3099//===----------------------------------------------------------------------===//
3100
3101/// Check that the construction parameters for a CastInst are correct. This
3102/// could be broken out into the separate constructors but it is useful to have
3103/// it in one place and to eliminate the redundant code for getting the sizes
3104/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003105bool
Chris Lattner229907c2011-07-18 04:54:35 +00003106CastInst::castIsValid(Instruction::CastOps op, Value *S, Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003107
3108 // Check for type sanity on the arguments
Chris Lattner229907c2011-07-18 04:54:35 +00003109 Type *SrcTy = S->getType();
Evan Cheng098d7b72013-01-10 23:22:53 +00003110
Chris Lattner37bc78a2010-01-26 21:51:43 +00003111 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType() ||
3112 SrcTy->isAggregateType() || DstTy->isAggregateType())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003113 return false;
3114
3115 // Get the size of the types in bits, we'll need this later
Dan Gohman7ccc52f2009-06-15 22:12:54 +00003116 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
3117 unsigned DstBitSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003118
Duncan Sands7f646562011-05-18 09:21:57 +00003119 // If these are vector types, get the lengths of the vectors (using zero for
3120 // scalar types means that checking that vector lengths match also checks that
3121 // scalars are not being converted to vectors or vectors to scalars).
3122 unsigned SrcLength = SrcTy->isVectorTy() ?
3123 cast<VectorType>(SrcTy)->getNumElements() : 0;
3124 unsigned DstLength = DstTy->isVectorTy() ?
3125 cast<VectorType>(DstTy)->getNumElements() : 0;
3126
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003127 // Switch on the opcode provided
3128 switch (op) {
3129 default: return false; // This is an input error
3130 case Instruction::Trunc:
Duncan Sands7f646562011-05-18 09:21:57 +00003131 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
3132 SrcLength == DstLength && SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003133 case Instruction::ZExt:
Duncan Sands7f646562011-05-18 09:21:57 +00003134 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
3135 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003136 case Instruction::SExt:
Duncan Sands7f646562011-05-18 09:21:57 +00003137 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
3138 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003139 case Instruction::FPTrunc:
Duncan Sands7f646562011-05-18 09:21:57 +00003140 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
3141 SrcLength == DstLength && SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003142 case Instruction::FPExt:
Duncan Sands7f646562011-05-18 09:21:57 +00003143 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
3144 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003145 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003146 case Instruction::SIToFP:
Duncan Sands7f646562011-05-18 09:21:57 +00003147 return SrcTy->isIntOrIntVectorTy() && DstTy->isFPOrFPVectorTy() &&
3148 SrcLength == DstLength;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003149 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003150 case Instruction::FPToSI:
Duncan Sands7f646562011-05-18 09:21:57 +00003151 return SrcTy->isFPOrFPVectorTy() && DstTy->isIntOrIntVectorTy() &&
3152 SrcLength == DstLength;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003153 case Instruction::PtrToInt:
Chris Lattner8a3df542012-01-25 01:32:59 +00003154 if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy))
Nadav Rotem3924cb02011-12-05 06:29:09 +00003155 return false;
Chris Lattner8a3df542012-01-25 01:32:59 +00003156 if (VectorType *VT = dyn_cast<VectorType>(SrcTy))
3157 if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements())
3158 return false;
Nadav Rotem3924cb02011-12-05 06:29:09 +00003159 return SrcTy->getScalarType()->isPointerTy() &&
3160 DstTy->getScalarType()->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003161 case Instruction::IntToPtr:
Chris Lattner8a3df542012-01-25 01:32:59 +00003162 if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy))
Nadav Rotem3924cb02011-12-05 06:29:09 +00003163 return false;
Chris Lattner8a3df542012-01-25 01:32:59 +00003164 if (VectorType *VT = dyn_cast<VectorType>(SrcTy))
3165 if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements())
3166 return false;
Nadav Rotem3924cb02011-12-05 06:29:09 +00003167 return SrcTy->getScalarType()->isIntegerTy() &&
3168 DstTy->getScalarType()->isPointerTy();
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003169 case Instruction::BitCast: {
3170 PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy->getScalarType());
3171 PointerType *DstPtrTy = dyn_cast<PointerType>(DstTy->getScalarType());
3172
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003173 // BitCast implies a no-op cast of type only. No bits change.
3174 // However, you can't cast pointers to anything but pointers.
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003175 if (!SrcPtrTy != !DstPtrTy)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003176 return false;
3177
Alp Tokerf907b892013-12-05 05:44:44 +00003178 // For non-pointer cases, the cast is okay if the source and destination bit
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003179 // widths are identical.
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003180 if (!SrcPtrTy)
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003181 return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits();
3182
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003183 // If both are pointers then the address spaces must match.
3184 if (SrcPtrTy->getAddressSpace() != DstPtrTy->getAddressSpace())
3185 return false;
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003186
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003187 // A vector of pointers must have the same number of elements.
3188 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) {
3189 if (VectorType *DstVecTy = dyn_cast<VectorType>(DstTy))
3190 return (SrcVecTy->getNumElements() == DstVecTy->getNumElements());
3191
3192 return false;
3193 }
3194
3195 return true;
3196 }
3197 case Instruction::AddrSpaceCast: {
3198 PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy->getScalarType());
3199 if (!SrcPtrTy)
3200 return false;
3201
3202 PointerType *DstPtrTy = dyn_cast<PointerType>(DstTy->getScalarType());
3203 if (!DstPtrTy)
3204 return false;
3205
3206 if (SrcPtrTy->getAddressSpace() == DstPtrTy->getAddressSpace())
3207 return false;
3208
3209 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) {
3210 if (VectorType *DstVecTy = dyn_cast<VectorType>(DstTy))
3211 return (SrcVecTy->getNumElements() == DstVecTy->getNumElements());
3212
3213 return false;
3214 }
3215
3216 return true;
3217 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003218 }
3219}
3220
3221TruncInst::TruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003222 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003223) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003224 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003225}
3226
3227TruncInst::TruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003228 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003229) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003230 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003231}
3232
3233ZExtInst::ZExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003234 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003235) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003236 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003237}
3238
3239ZExtInst::ZExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003240 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003241) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003242 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003243}
3244SExtInst::SExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003245 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003246) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003247 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003248}
3249
Jeff Cohencc08c832006-12-02 02:22:01 +00003250SExtInst::SExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003251 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003252) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003253 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003254}
3255
3256FPTruncInst::FPTruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003257 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003258) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003259 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003260}
3261
3262FPTruncInst::FPTruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003263 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003264) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003265 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003266}
3267
3268FPExtInst::FPExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003269 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003270) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003271 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003272}
3273
3274FPExtInst::FPExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003275 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003276) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003277 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003278}
3279
3280UIToFPInst::UIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003281 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003282) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003283 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003284}
3285
3286UIToFPInst::UIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003287 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003288) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003289 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003290}
3291
3292SIToFPInst::SIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003293 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003294) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003295 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003296}
3297
3298SIToFPInst::SIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003299 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003300) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003301 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003302}
3303
3304FPToUIInst::FPToUIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003305 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003306) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003307 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003308}
3309
3310FPToUIInst::FPToUIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003311 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003312) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003313 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003314}
3315
3316FPToSIInst::FPToSIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003317 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003318) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003319 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003320}
3321
3322FPToSIInst::FPToSIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003323 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003324) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003325 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003326}
3327
3328PtrToIntInst::PtrToIntInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003329 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003330) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003331 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003332}
3333
3334PtrToIntInst::PtrToIntInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003335 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003336) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003337 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003338}
3339
3340IntToPtrInst::IntToPtrInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003341 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003342) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003343 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003344}
3345
3346IntToPtrInst::IntToPtrInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003347 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003348) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003349 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003350}
3351
3352BitCastInst::BitCastInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003353 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003354) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003355 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003356}
3357
3358BitCastInst::BitCastInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003359 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003360) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003361 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003362}
Chris Lattnerf16dc002006-09-17 19:29:56 +00003363
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003364AddrSpaceCastInst::AddrSpaceCastInst(
3365 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
3366) : CastInst(Ty, AddrSpaceCast, S, Name, InsertBefore) {
3367 assert(castIsValid(getOpcode(), S, Ty) && "Illegal AddrSpaceCast");
3368}
3369
3370AddrSpaceCastInst::AddrSpaceCastInst(
3371 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
3372) : CastInst(Ty, AddrSpaceCast, S, Name, InsertAtEnd) {
3373 assert(castIsValid(getOpcode(), S, Ty) && "Illegal AddrSpaceCast");
3374}
3375
Chris Lattnerf16dc002006-09-17 19:29:56 +00003376//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00003377// CmpInst Classes
3378//===----------------------------------------------------------------------===//
3379
Craig Topper3186c012012-09-23 02:12:10 +00003380void CmpInst::anchor() {}
Chris Lattneraec33da2010-01-22 06:25:37 +00003381
Craig Topper1c3f2832015-12-15 06:11:33 +00003382CmpInst::CmpInst(Type *ty, OtherOps op, Predicate predicate, Value *LHS,
3383 Value *RHS, const Twine &Name, Instruction *InsertBefore)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00003384 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00003385 OperandTraits<CmpInst>::op_begin(this),
3386 OperandTraits<CmpInst>::operands(this),
3387 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003388 Op<0>() = LHS;
3389 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00003390 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00003391 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003392}
Gabor Greiff6caff662008-05-10 08:32:32 +00003393
Craig Topper1c3f2832015-12-15 06:11:33 +00003394CmpInst::CmpInst(Type *ty, OtherOps op, Predicate predicate, Value *LHS,
3395 Value *RHS, const Twine &Name, BasicBlock *InsertAtEnd)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00003396 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00003397 OperandTraits<CmpInst>::op_begin(this),
3398 OperandTraits<CmpInst>::operands(this),
3399 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003400 Op<0>() = LHS;
3401 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00003402 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00003403 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003404}
3405
3406CmpInst *
Craig Topper1c3f2832015-12-15 06:11:33 +00003407CmpInst::Create(OtherOps Op, Predicate predicate, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00003408 const Twine &Name, Instruction *InsertBefore) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003409 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003410 if (InsertBefore)
3411 return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate),
3412 S1, S2, Name);
3413 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003414 return new ICmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003415 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003416 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003417
3418 if (InsertBefore)
3419 return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate),
3420 S1, S2, Name);
3421 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003422 return new FCmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003423 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003424}
3425
3426CmpInst *
Craig Topper1c3f2832015-12-15 06:11:33 +00003427CmpInst::Create(OtherOps Op, Predicate predicate, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00003428 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003429 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003430 return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
3431 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003432 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003433 return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
3434 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003435}
3436
3437void CmpInst::swapOperands() {
3438 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
3439 IC->swapOperands();
3440 else
3441 cast<FCmpInst>(this)->swapOperands();
3442}
3443
Duncan Sands95c4ecc2011-01-04 12:52:29 +00003444bool CmpInst::isCommutative() const {
3445 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00003446 return IC->isCommutative();
3447 return cast<FCmpInst>(this)->isCommutative();
3448}
3449
Duncan Sands95c4ecc2011-01-04 12:52:29 +00003450bool CmpInst::isEquality() const {
3451 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00003452 return IC->isEquality();
3453 return cast<FCmpInst>(this)->isEquality();
3454}
3455
3456
Dan Gohman4e724382008-05-31 02:47:54 +00003457CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003458 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003459 default: llvm_unreachable("Unknown cmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00003460 case ICMP_EQ: return ICMP_NE;
3461 case ICMP_NE: return ICMP_EQ;
3462 case ICMP_UGT: return ICMP_ULE;
3463 case ICMP_ULT: return ICMP_UGE;
3464 case ICMP_UGE: return ICMP_ULT;
3465 case ICMP_ULE: return ICMP_UGT;
3466 case ICMP_SGT: return ICMP_SLE;
3467 case ICMP_SLT: return ICMP_SGE;
3468 case ICMP_SGE: return ICMP_SLT;
3469 case ICMP_SLE: return ICMP_SGT;
Reid Spencerd9436b62006-11-20 01:22:35 +00003470
Dan Gohman4e724382008-05-31 02:47:54 +00003471 case FCMP_OEQ: return FCMP_UNE;
3472 case FCMP_ONE: return FCMP_UEQ;
3473 case FCMP_OGT: return FCMP_ULE;
3474 case FCMP_OLT: return FCMP_UGE;
3475 case FCMP_OGE: return FCMP_ULT;
3476 case FCMP_OLE: return FCMP_UGT;
3477 case FCMP_UEQ: return FCMP_ONE;
3478 case FCMP_UNE: return FCMP_OEQ;
3479 case FCMP_UGT: return FCMP_OLE;
3480 case FCMP_ULT: return FCMP_OGE;
3481 case FCMP_UGE: return FCMP_OLT;
3482 case FCMP_ULE: return FCMP_OGT;
3483 case FCMP_ORD: return FCMP_UNO;
3484 case FCMP_UNO: return FCMP_ORD;
3485 case FCMP_TRUE: return FCMP_FALSE;
3486 case FCMP_FALSE: return FCMP_TRUE;
Reid Spencerd9436b62006-11-20 01:22:35 +00003487 }
3488}
3489
Tim Northoverde3aea0412016-08-17 20:25:25 +00003490StringRef CmpInst::getPredicateName(Predicate Pred) {
3491 switch (Pred) {
3492 default: return "unknown";
3493 case FCmpInst::FCMP_FALSE: return "false";
3494 case FCmpInst::FCMP_OEQ: return "oeq";
3495 case FCmpInst::FCMP_OGT: return "ogt";
3496 case FCmpInst::FCMP_OGE: return "oge";
3497 case FCmpInst::FCMP_OLT: return "olt";
3498 case FCmpInst::FCMP_OLE: return "ole";
3499 case FCmpInst::FCMP_ONE: return "one";
3500 case FCmpInst::FCMP_ORD: return "ord";
3501 case FCmpInst::FCMP_UNO: return "uno";
3502 case FCmpInst::FCMP_UEQ: return "ueq";
3503 case FCmpInst::FCMP_UGT: return "ugt";
3504 case FCmpInst::FCMP_UGE: return "uge";
3505 case FCmpInst::FCMP_ULT: return "ult";
3506 case FCmpInst::FCMP_ULE: return "ule";
3507 case FCmpInst::FCMP_UNE: return "une";
3508 case FCmpInst::FCMP_TRUE: return "true";
3509 case ICmpInst::ICMP_EQ: return "eq";
3510 case ICmpInst::ICMP_NE: return "ne";
3511 case ICmpInst::ICMP_SGT: return "sgt";
3512 case ICmpInst::ICMP_SGE: return "sge";
3513 case ICmpInst::ICMP_SLT: return "slt";
3514 case ICmpInst::ICMP_SLE: return "sle";
3515 case ICmpInst::ICMP_UGT: return "ugt";
3516 case ICmpInst::ICMP_UGE: return "uge";
3517 case ICmpInst::ICMP_ULT: return "ult";
3518 case ICmpInst::ICMP_ULE: return "ule";
3519 }
3520}
3521
Pete Cooper1d078692015-12-14 20:29:16 +00003522void ICmpInst::anchor() {}
3523
Reid Spencer266e42b2006-12-23 06:05:41 +00003524ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
3525 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003526 default: llvm_unreachable("Unknown icmp predicate!");
Reid Spencer266e42b2006-12-23 06:05:41 +00003527 case ICMP_EQ: case ICMP_NE:
3528 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
3529 return pred;
3530 case ICMP_UGT: return ICMP_SGT;
3531 case ICMP_ULT: return ICMP_SLT;
3532 case ICMP_UGE: return ICMP_SGE;
3533 case ICMP_ULE: return ICMP_SLE;
3534 }
3535}
3536
Nick Lewycky8ea81e82008-01-28 03:48:02 +00003537ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
3538 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003539 default: llvm_unreachable("Unknown icmp predicate!");
Nick Lewycky8ea81e82008-01-28 03:48:02 +00003540 case ICMP_EQ: case ICMP_NE:
3541 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
3542 return pred;
3543 case ICMP_SGT: return ICMP_UGT;
3544 case ICMP_SLT: return ICMP_ULT;
3545 case ICMP_SGE: return ICMP_UGE;
3546 case ICMP_SLE: return ICMP_ULE;
3547 }
3548}
3549
Dan Gohman4e724382008-05-31 02:47:54 +00003550CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003551 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003552 default: llvm_unreachable("Unknown cmp predicate!");
Dan Gohman4e724382008-05-31 02:47:54 +00003553 case ICMP_EQ: case ICMP_NE:
3554 return pred;
3555 case ICMP_SGT: return ICMP_SLT;
3556 case ICMP_SLT: return ICMP_SGT;
3557 case ICMP_SGE: return ICMP_SLE;
3558 case ICMP_SLE: return ICMP_SGE;
3559 case ICMP_UGT: return ICMP_ULT;
3560 case ICMP_ULT: return ICMP_UGT;
3561 case ICMP_UGE: return ICMP_ULE;
3562 case ICMP_ULE: return ICMP_UGE;
3563
Reid Spencerd9436b62006-11-20 01:22:35 +00003564 case FCMP_FALSE: case FCMP_TRUE:
3565 case FCMP_OEQ: case FCMP_ONE:
3566 case FCMP_UEQ: case FCMP_UNE:
3567 case FCMP_ORD: case FCMP_UNO:
3568 return pred;
3569 case FCMP_OGT: return FCMP_OLT;
3570 case FCMP_OLT: return FCMP_OGT;
3571 case FCMP_OGE: return FCMP_OLE;
3572 case FCMP_OLE: return FCMP_OGE;
3573 case FCMP_UGT: return FCMP_ULT;
3574 case FCMP_ULT: return FCMP_UGT;
3575 case FCMP_UGE: return FCMP_ULE;
3576 case FCMP_ULE: return FCMP_UGE;
3577 }
3578}
3579
Sanjoy Das6e78b172015-10-22 19:57:34 +00003580CmpInst::Predicate CmpInst::getSignedPredicate(Predicate pred) {
3581 assert(CmpInst::isUnsigned(pred) && "Call only with signed predicates!");
3582
3583 switch (pred) {
3584 default:
3585 llvm_unreachable("Unknown predicate!");
3586 case CmpInst::ICMP_ULT:
3587 return CmpInst::ICMP_SLT;
3588 case CmpInst::ICMP_ULE:
3589 return CmpInst::ICMP_SLE;
3590 case CmpInst::ICMP_UGT:
3591 return CmpInst::ICMP_SGT;
3592 case CmpInst::ICMP_UGE:
3593 return CmpInst::ICMP_SGE;
3594 }
3595}
3596
Craig Topper1c3f2832015-12-15 06:11:33 +00003597bool CmpInst::isUnsigned(Predicate predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003598 switch (predicate) {
3599 default: return false;
3600 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
3601 case ICmpInst::ICMP_UGE: return true;
3602 }
3603}
3604
Craig Topper1c3f2832015-12-15 06:11:33 +00003605bool CmpInst::isSigned(Predicate predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003606 switch (predicate) {
3607 default: return false;
3608 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
3609 case ICmpInst::ICMP_SGE: return true;
3610 }
3611}
3612
Craig Topper1c3f2832015-12-15 06:11:33 +00003613bool CmpInst::isOrdered(Predicate predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003614 switch (predicate) {
3615 default: return false;
3616 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
3617 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
3618 case FCmpInst::FCMP_ORD: return true;
3619 }
3620}
3621
Craig Topper1c3f2832015-12-15 06:11:33 +00003622bool CmpInst::isUnordered(Predicate predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003623 switch (predicate) {
3624 default: return false;
3625 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
3626 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
3627 case FCmpInst::FCMP_UNO: return true;
3628 }
3629}
3630
Craig Topper1c3f2832015-12-15 06:11:33 +00003631bool CmpInst::isTrueWhenEqual(Predicate predicate) {
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003632 switch(predicate) {
3633 default: return false;
3634 case ICMP_EQ: case ICMP_UGE: case ICMP_ULE: case ICMP_SGE: case ICMP_SLE:
3635 case FCMP_TRUE: case FCMP_UEQ: case FCMP_UGE: case FCMP_ULE: return true;
3636 }
3637}
3638
Craig Topper1c3f2832015-12-15 06:11:33 +00003639bool CmpInst::isFalseWhenEqual(Predicate predicate) {
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003640 switch(predicate) {
3641 case ICMP_NE: case ICMP_UGT: case ICMP_ULT: case ICMP_SGT: case ICMP_SLT:
3642 case FCMP_FALSE: case FCMP_ONE: case FCMP_OGT: case FCMP_OLT: return true;
3643 default: return false;
3644 }
3645}
3646
Chad Rosier99bc4802016-04-21 16:18:02 +00003647bool CmpInst::isImpliedTrueByMatchingCmp(Predicate Pred1, Predicate Pred2) {
Chad Rosieraf83e402016-04-21 14:04:54 +00003648 // If the predicates match, then we know the first condition implies the
3649 // second is true.
3650 if (Pred1 == Pred2)
3651 return true;
3652
3653 switch (Pred1) {
3654 default:
3655 break;
Chad Rosier1a601592016-04-22 17:57:34 +00003656 case ICMP_EQ:
Chad Rosier3d75f8c2016-04-25 13:25:14 +00003657 // A == B implies A >=u B, A <=u B, A >=s B, and A <=s B are true.
Chad Rosier1a601592016-04-22 17:57:34 +00003658 return Pred2 == ICMP_UGE || Pred2 == ICMP_ULE || Pred2 == ICMP_SGE ||
3659 Pred2 == ICMP_SLE;
Chad Rosier3456cb52016-04-22 17:14:12 +00003660 case ICMP_UGT: // A >u B implies A != B and A >=u B are true.
3661 return Pred2 == ICMP_NE || Pred2 == ICMP_UGE;
3662 case ICMP_ULT: // A <u B implies A != B and A <=u B are true.
3663 return Pred2 == ICMP_NE || Pred2 == ICMP_ULE;
3664 case ICMP_SGT: // A >s B implies A != B and A >=s B are true.
3665 return Pred2 == ICMP_NE || Pred2 == ICMP_SGE;
3666 case ICMP_SLT: // A <s B implies A != B and A <=s B are true.
3667 return Pred2 == ICMP_NE || Pred2 == ICMP_SLE;
Chad Rosieraf83e402016-04-21 14:04:54 +00003668 }
3669 return false;
3670}
3671
Chad Rosier99bc4802016-04-21 16:18:02 +00003672bool CmpInst::isImpliedFalseByMatchingCmp(Predicate Pred1, Predicate Pred2) {
Chad Rosier1a601592016-04-22 17:57:34 +00003673 return isImpliedTrueByMatchingCmp(Pred1, getInversePredicate(Pred2));
Chad Rosieraf83e402016-04-21 14:04:54 +00003674}
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003675
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003676//===----------------------------------------------------------------------===//
3677// SwitchInst Implementation
3678//===----------------------------------------------------------------------===//
3679
Chris Lattnerbaf00152010-11-17 05:41:46 +00003680void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumReserved) {
3681 assert(Value && Default && NumReserved);
3682 ReservedSpace = NumReserved;
Pete Cooperb4eede22015-06-12 17:48:10 +00003683 setNumHungOffUseOperands(2);
Pete Cooper3fc30402015-06-10 22:38:46 +00003684 allocHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003685
Pete Coopereb31b682015-05-21 22:48:54 +00003686 Op<0>() = Value;
3687 Op<1>() = Default;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003688}
3689
Chris Lattner2195fc42007-02-24 00:55:48 +00003690/// SwitchInst ctor - Create a new switch instruction, specifying a value to
3691/// switch on and a default destination. The number of additional cases can
3692/// be specified here to make memory allocation more efficient. This
3693/// constructor can also autoinsert before another instruction.
3694SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3695 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00003696 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
Craig Topperc6207612014-04-09 06:08:46 +00003697 nullptr, 0, InsertBefore) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003698 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00003699}
3700
3701/// SwitchInst ctor - Create a new switch instruction, specifying a value to
3702/// switch on and a default destination. The number of additional cases can
3703/// be specified here to make memory allocation more efficient. This
3704/// constructor also autoinserts at the end of the specified BasicBlock.
3705SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3706 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00003707 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
Craig Topperc6207612014-04-09 06:08:46 +00003708 nullptr, 0, InsertAtEnd) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003709 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00003710}
3711
Misha Brukmanb1c93172005-04-21 23:48:37 +00003712SwitchInst::SwitchInst(const SwitchInst &SI)
Craig Topperc6207612014-04-09 06:08:46 +00003713 : TerminatorInst(SI.getType(), Instruction::Switch, nullptr, 0) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003714 init(SI.getCondition(), SI.getDefaultDest(), SI.getNumOperands());
Pete Cooperb4eede22015-06-12 17:48:10 +00003715 setNumHungOffUseOperands(SI.getNumOperands());
Pete Cooper74510a42015-06-12 17:48:05 +00003716 Use *OL = getOperandList();
3717 const Use *InOL = SI.getOperandList();
Chris Lattnerbaf00152010-11-17 05:41:46 +00003718 for (unsigned i = 2, E = SI.getNumOperands(); i != E; i += 2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003719 OL[i] = InOL[i];
3720 OL[i+1] = InOL[i+1];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003721 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003722 SubclassOptionalData = SI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003723}
3724
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003725
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003726/// addCase - Add an entry to the switch instruction...
3727///
Chris Lattner47ac1872005-02-24 05:32:09 +00003728void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Pete Cooperb4eede22015-06-12 17:48:10 +00003729 unsigned NewCaseIdx = getNumCases();
3730 unsigned OpNo = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003731 if (OpNo+2 > ReservedSpace)
Jay Foade98f29d2011-04-01 08:00:58 +00003732 growOperands(); // Get more space!
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003733 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00003734 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Pete Cooperb4eede22015-06-12 17:48:10 +00003735 setNumHungOffUseOperands(OpNo+2);
Chandler Carruth927d8e62017-04-12 07:27:28 +00003736 CaseHandle Case(this, NewCaseIdx);
Bob Wilsone4077362013-09-09 19:14:35 +00003737 Case.setValue(OnVal);
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00003738 Case.setSuccessor(Dest);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003739}
3740
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003741/// removeCase - This method removes the specified case and its successor
3742/// from the switch instruction.
Chandler Carruth927d8e62017-04-12 07:27:28 +00003743SwitchInst::CaseIt SwitchInst::removeCase(CaseIt I) {
3744 unsigned idx = I->getCaseIndex();
3745
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003746 assert(2 + idx*2 < getNumOperands() && "Case index out of range!!!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003747
3748 unsigned NumOps = getNumOperands();
Pete Cooper74510a42015-06-12 17:48:05 +00003749 Use *OL = getOperandList();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003750
Jay Foad14277722011-02-01 09:22:34 +00003751 // Overwrite this case with the end of the list.
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003752 if (2 + (idx + 1) * 2 != NumOps) {
3753 OL[2 + idx * 2] = OL[NumOps - 2];
3754 OL[2 + idx * 2 + 1] = OL[NumOps - 1];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003755 }
3756
3757 // Nuke the last value.
Craig Topperc6207612014-04-09 06:08:46 +00003758 OL[NumOps-2].set(nullptr);
3759 OL[NumOps-2+1].set(nullptr);
Pete Cooperb4eede22015-06-12 17:48:10 +00003760 setNumHungOffUseOperands(NumOps-2);
Chandler Carruth0d256c02017-03-26 02:49:23 +00003761
3762 return CaseIt(this, idx);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003763}
3764
Jay Foade98f29d2011-04-01 08:00:58 +00003765/// growOperands - grow operands - This grows the operand list in response
3766/// to a push_back style of operation. This grows the number of ops by 3 times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003767///
Jay Foade98f29d2011-04-01 08:00:58 +00003768void SwitchInst::growOperands() {
Gabor Greiff6caff662008-05-10 08:32:32 +00003769 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +00003770 unsigned NumOps = e*3;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003771
3772 ReservedSpace = NumOps;
Pete Cooper93f9ff52015-06-10 22:38:41 +00003773 growHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003774}
3775
3776
3777BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
3778 return getSuccessor(idx);
3779}
3780unsigned SwitchInst::getNumSuccessorsV() const {
3781 return getNumSuccessors();
3782}
3783void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
3784 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003785}
Chris Lattnerf22be932004-10-15 23:52:53 +00003786
Chris Lattner3ed871f2009-10-27 19:13:16 +00003787//===----------------------------------------------------------------------===//
Jay Foadbbb91f22011-01-16 15:30:52 +00003788// IndirectBrInst Implementation
Chris Lattner3ed871f2009-10-27 19:13:16 +00003789//===----------------------------------------------------------------------===//
3790
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003791void IndirectBrInst::init(Value *Address, unsigned NumDests) {
Duncan Sands19d0b472010-02-16 11:11:14 +00003792 assert(Address && Address->getType()->isPointerTy() &&
Chris Lattner6747b4c2009-10-29 05:53:32 +00003793 "Address of indirectbr must be a pointer");
Chris Lattner3ed871f2009-10-27 19:13:16 +00003794 ReservedSpace = 1+NumDests;
Pete Cooperb4eede22015-06-12 17:48:10 +00003795 setNumHungOffUseOperands(1);
Pete Cooper3fc30402015-06-10 22:38:46 +00003796 allocHungoffUses(ReservedSpace);
3797
Pete Coopereb31b682015-05-21 22:48:54 +00003798 Op<0>() = Address;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003799}
3800
3801
Jay Foade98f29d2011-04-01 08:00:58 +00003802/// growOperands - grow operands - This grows the operand list in response
3803/// to a push_back style of operation. This grows the number of ops by 2 times.
Chris Lattner3ed871f2009-10-27 19:13:16 +00003804///
Jay Foade98f29d2011-04-01 08:00:58 +00003805void IndirectBrInst::growOperands() {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003806 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +00003807 unsigned NumOps = e*2;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003808
3809 ReservedSpace = NumOps;
Pete Cooper93f9ff52015-06-10 22:38:41 +00003810 growHungoffUses(ReservedSpace);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003811}
3812
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003813IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3814 Instruction *InsertBefore)
3815: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Craig Topperc6207612014-04-09 06:08:46 +00003816 nullptr, 0, InsertBefore) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003817 init(Address, NumCases);
3818}
3819
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003820IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3821 BasicBlock *InsertAtEnd)
3822: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Craig Topperc6207612014-04-09 06:08:46 +00003823 nullptr, 0, InsertAtEnd) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003824 init(Address, NumCases);
3825}
3826
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003827IndirectBrInst::IndirectBrInst(const IndirectBrInst &IBI)
Pete Cooper3fc30402015-06-10 22:38:46 +00003828 : TerminatorInst(Type::getVoidTy(IBI.getContext()), Instruction::IndirectBr,
3829 nullptr, IBI.getNumOperands()) {
3830 allocHungoffUses(IBI.getNumOperands());
Pete Cooper74510a42015-06-12 17:48:05 +00003831 Use *OL = getOperandList();
3832 const Use *InOL = IBI.getOperandList();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003833 for (unsigned i = 0, E = IBI.getNumOperands(); i != E; ++i)
3834 OL[i] = InOL[i];
3835 SubclassOptionalData = IBI.SubclassOptionalData;
3836}
3837
Chris Lattner3ed871f2009-10-27 19:13:16 +00003838/// addDestination - Add a destination.
3839///
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003840void IndirectBrInst::addDestination(BasicBlock *DestBB) {
Pete Cooperb4eede22015-06-12 17:48:10 +00003841 unsigned OpNo = getNumOperands();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003842 if (OpNo+1 > ReservedSpace)
Jay Foade98f29d2011-04-01 08:00:58 +00003843 growOperands(); // Get more space!
Chris Lattner3ed871f2009-10-27 19:13:16 +00003844 // Initialize some new operands.
3845 assert(OpNo < ReservedSpace && "Growing didn't work!");
Pete Cooperb4eede22015-06-12 17:48:10 +00003846 setNumHungOffUseOperands(OpNo+1);
Pete Cooper74510a42015-06-12 17:48:05 +00003847 getOperandList()[OpNo] = DestBB;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003848}
3849
3850/// removeDestination - This method removes the specified successor from the
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003851/// indirectbr instruction.
3852void IndirectBrInst::removeDestination(unsigned idx) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003853 assert(idx < getNumOperands()-1 && "Successor index out of range!");
3854
3855 unsigned NumOps = getNumOperands();
Pete Cooper74510a42015-06-12 17:48:05 +00003856 Use *OL = getOperandList();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003857
3858 // Replace this value with the last one.
3859 OL[idx+1] = OL[NumOps-1];
3860
3861 // Nuke the last value.
Craig Topperc6207612014-04-09 06:08:46 +00003862 OL[NumOps-1].set(nullptr);
Pete Cooperb4eede22015-06-12 17:48:10 +00003863 setNumHungOffUseOperands(NumOps-1);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003864}
3865
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003866BasicBlock *IndirectBrInst::getSuccessorV(unsigned idx) const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003867 return getSuccessor(idx);
3868}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003869unsigned IndirectBrInst::getNumSuccessorsV() const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003870 return getNumSuccessors();
3871}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003872void IndirectBrInst::setSuccessorV(unsigned idx, BasicBlock *B) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003873 setSuccessor(idx, B);
3874}
3875
3876//===----------------------------------------------------------------------===//
Pete Cooper75403d72015-06-24 20:22:23 +00003877// cloneImpl() implementations
Chris Lattner3ed871f2009-10-27 19:13:16 +00003878//===----------------------------------------------------------------------===//
3879
Chris Lattnerf22be932004-10-15 23:52:53 +00003880// Define these methods here so vtables don't get emitted into every translation
3881// unit that uses these classes.
3882
Pete Cooper75403d72015-06-24 20:22:23 +00003883GetElementPtrInst *GetElementPtrInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003884 return new (getNumOperands()) GetElementPtrInst(*this);
Chris Lattnerf22be932004-10-15 23:52:53 +00003885}
3886
Pete Cooper75403d72015-06-24 20:22:23 +00003887BinaryOperator *BinaryOperator::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003888 return Create(getOpcode(), Op<0>(), Op<1>());
Chris Lattnerf22be932004-10-15 23:52:53 +00003889}
3890
Pete Cooper75403d72015-06-24 20:22:23 +00003891FCmpInst *FCmpInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003892 return new FCmpInst(getPredicate(), Op<0>(), Op<1>());
Reid Spencerd9436b62006-11-20 01:22:35 +00003893}
3894
Pete Cooper75403d72015-06-24 20:22:23 +00003895ICmpInst *ICmpInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003896 return new ICmpInst(getPredicate(), Op<0>(), Op<1>());
Dan Gohman0752bff2008-05-23 00:36:11 +00003897}
3898
Pete Cooper75403d72015-06-24 20:22:23 +00003899ExtractValueInst *ExtractValueInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003900 return new ExtractValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003901}
3902
Pete Cooper75403d72015-06-24 20:22:23 +00003903InsertValueInst *InsertValueInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003904 return new InsertValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003905}
3906
Pete Cooper75403d72015-06-24 20:22:23 +00003907AllocaInst *AllocaInst::cloneImpl() const {
David Majnemer6b3244c2014-04-30 16:12:21 +00003908 AllocaInst *Result = new AllocaInst(getAllocatedType(),
Matt Arsenault3c1fc762017-04-10 22:27:50 +00003909 getType()->getAddressSpace(),
David Majnemer6b3244c2014-04-30 16:12:21 +00003910 (Value *)getOperand(0), getAlignment());
3911 Result->setUsedWithInAlloca(isUsedWithInAlloca());
Manman Ren9bfd0d02016-04-01 21:41:15 +00003912 Result->setSwiftError(isSwiftError());
David Majnemer6b3244c2014-04-30 16:12:21 +00003913 return Result;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003914}
3915
Pete Cooper75403d72015-06-24 20:22:23 +00003916LoadInst *LoadInst::cloneImpl() const {
Eli Friedman59b66882011-08-09 23:02:53 +00003917 return new LoadInst(getOperand(0), Twine(), isVolatile(),
3918 getAlignment(), getOrdering(), getSynchScope());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003919}
3920
Pete Cooper75403d72015-06-24 20:22:23 +00003921StoreInst *StoreInst::cloneImpl() const {
Eli Friedmancad9f2a2011-08-10 17:39:11 +00003922 return new StoreInst(getOperand(0), getOperand(1), isVolatile(),
Eli Friedman59b66882011-08-09 23:02:53 +00003923 getAlignment(), getOrdering(), getSynchScope());
3924
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003925}
3926
Pete Cooper75403d72015-06-24 20:22:23 +00003927AtomicCmpXchgInst *AtomicCmpXchgInst::cloneImpl() const {
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003928 AtomicCmpXchgInst *Result =
3929 new AtomicCmpXchgInst(getOperand(0), getOperand(1), getOperand(2),
Tim Northovere94a5182014-03-11 10:48:52 +00003930 getSuccessOrdering(), getFailureOrdering(),
3931 getSynchScope());
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003932 Result->setVolatile(isVolatile());
Tim Northover420a2162014-06-13 14:24:07 +00003933 Result->setWeak(isWeak());
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003934 return Result;
3935}
3936
Pete Cooper75403d72015-06-24 20:22:23 +00003937AtomicRMWInst *AtomicRMWInst::cloneImpl() const {
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003938 AtomicRMWInst *Result =
3939 new AtomicRMWInst(getOperation(),getOperand(0), getOperand(1),
3940 getOrdering(), getSynchScope());
3941 Result->setVolatile(isVolatile());
3942 return Result;
3943}
3944
Pete Cooper75403d72015-06-24 20:22:23 +00003945FenceInst *FenceInst::cloneImpl() const {
Eli Friedmanfee02c62011-07-25 23:16:38 +00003946 return new FenceInst(getContext(), getOrdering(), getSynchScope());
3947}
3948
Pete Cooper75403d72015-06-24 20:22:23 +00003949TruncInst *TruncInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003950 return new TruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003951}
3952
Pete Cooper75403d72015-06-24 20:22:23 +00003953ZExtInst *ZExtInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003954 return new ZExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003955}
3956
Pete Cooper75403d72015-06-24 20:22:23 +00003957SExtInst *SExtInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003958 return new SExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003959}
3960
Pete Cooper75403d72015-06-24 20:22:23 +00003961FPTruncInst *FPTruncInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003962 return new FPTruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003963}
3964
Pete Cooper75403d72015-06-24 20:22:23 +00003965FPExtInst *FPExtInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003966 return new FPExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003967}
3968
Pete Cooper75403d72015-06-24 20:22:23 +00003969UIToFPInst *UIToFPInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003970 return new UIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003971}
3972
Pete Cooper75403d72015-06-24 20:22:23 +00003973SIToFPInst *SIToFPInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003974 return new SIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003975}
3976
Pete Cooper75403d72015-06-24 20:22:23 +00003977FPToUIInst *FPToUIInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003978 return new FPToUIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003979}
3980
Pete Cooper75403d72015-06-24 20:22:23 +00003981FPToSIInst *FPToSIInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003982 return new FPToSIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003983}
3984
Pete Cooper75403d72015-06-24 20:22:23 +00003985PtrToIntInst *PtrToIntInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003986 return new PtrToIntInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003987}
3988
Pete Cooper75403d72015-06-24 20:22:23 +00003989IntToPtrInst *IntToPtrInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003990 return new IntToPtrInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003991}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003992
Pete Cooper75403d72015-06-24 20:22:23 +00003993BitCastInst *BitCastInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003994 return new BitCastInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003995}
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003996
Pete Cooper75403d72015-06-24 20:22:23 +00003997AddrSpaceCastInst *AddrSpaceCastInst::cloneImpl() const {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003998 return new AddrSpaceCastInst(getOperand(0), getType());
3999}
4000
Pete Cooper75403d72015-06-24 20:22:23 +00004001CallInst *CallInst::cloneImpl() const {
Sanjoy Dasbd1c1bf2015-11-10 20:13:21 +00004002 if (hasOperandBundles()) {
4003 unsigned DescriptorBytes = getNumOperandBundles() * sizeof(BundleOpInfo);
4004 return new(getNumOperands(), DescriptorBytes) CallInst(*this);
4005 }
Devang Patel11cf3f42009-10-27 22:16:29 +00004006 return new(getNumOperands()) CallInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004007}
4008
Pete Cooper75403d72015-06-24 20:22:23 +00004009SelectInst *SelectInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00004010 return SelectInst::Create(getOperand(0), getOperand(1), getOperand(2));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00004011}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004012
Pete Cooper75403d72015-06-24 20:22:23 +00004013VAArgInst *VAArgInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00004014 return new VAArgInst(getOperand(0), getType());
Chris Lattnerbbe0a422006-04-08 01:18:18 +00004015}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004016
Pete Cooper75403d72015-06-24 20:22:23 +00004017ExtractElementInst *ExtractElementInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00004018 return ExtractElementInst::Create(getOperand(0), getOperand(1));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00004019}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004020
Pete Cooper75403d72015-06-24 20:22:23 +00004021InsertElementInst *InsertElementInst::cloneImpl() const {
Chris Lattner1dcb6542012-01-25 23:49:49 +00004022 return InsertElementInst::Create(getOperand(0), getOperand(1), getOperand(2));
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004023}
4024
Pete Cooper75403d72015-06-24 20:22:23 +00004025ShuffleVectorInst *ShuffleVectorInst::cloneImpl() const {
Chris Lattner1dcb6542012-01-25 23:49:49 +00004026 return new ShuffleVectorInst(getOperand(0), getOperand(1), getOperand(2));
Gabor Greif697e94c2008-05-15 10:04:30 +00004027}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004028
Pete Cooper75403d72015-06-24 20:22:23 +00004029PHINode *PHINode::cloneImpl() const { return new PHINode(*this); }
Devang Patel11cf3f42009-10-27 22:16:29 +00004030
Pete Cooper75403d72015-06-24 20:22:23 +00004031LandingPadInst *LandingPadInst::cloneImpl() const {
Bill Wendlingfae14752011-08-12 20:24:12 +00004032 return new LandingPadInst(*this);
4033}
4034
Pete Cooper75403d72015-06-24 20:22:23 +00004035ReturnInst *ReturnInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00004036 return new(getNumOperands()) ReturnInst(*this);
4037}
4038
Pete Cooper75403d72015-06-24 20:22:23 +00004039BranchInst *BranchInst::cloneImpl() const {
Jay Foadd81f3c92011-01-07 20:29:02 +00004040 return new(getNumOperands()) BranchInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00004041}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004042
Pete Cooper75403d72015-06-24 20:22:23 +00004043SwitchInst *SwitchInst::cloneImpl() const { return new SwitchInst(*this); }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004044
Pete Cooper75403d72015-06-24 20:22:23 +00004045IndirectBrInst *IndirectBrInst::cloneImpl() const {
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00004046 return new IndirectBrInst(*this);
Chris Lattner3ed871f2009-10-27 19:13:16 +00004047}
4048
Pete Cooper75403d72015-06-24 20:22:23 +00004049InvokeInst *InvokeInst::cloneImpl() const {
Sanjoy Dasbd1c1bf2015-11-10 20:13:21 +00004050 if (hasOperandBundles()) {
4051 unsigned DescriptorBytes = getNumOperandBundles() * sizeof(BundleOpInfo);
4052 return new(getNumOperands(), DescriptorBytes) InvokeInst(*this);
4053 }
Devang Patel11cf3f42009-10-27 22:16:29 +00004054 return new(getNumOperands()) InvokeInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00004055}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004056
Pete Cooper75403d72015-06-24 20:22:23 +00004057ResumeInst *ResumeInst::cloneImpl() const { return new (1) ResumeInst(*this); }
Bill Wendlingf891bf82011-07-31 06:30:59 +00004058
David Majnemer654e1302015-07-31 17:58:14 +00004059CleanupReturnInst *CleanupReturnInst::cloneImpl() const {
4060 return new (getNumOperands()) CleanupReturnInst(*this);
4061}
4062
David Majnemer654e1302015-07-31 17:58:14 +00004063CatchReturnInst *CatchReturnInst::cloneImpl() const {
David Majnemer0bc0eef2015-08-15 02:46:08 +00004064 return new (getNumOperands()) CatchReturnInst(*this);
David Majnemer654e1302015-07-31 17:58:14 +00004065}
4066
David Majnemer8a1c45d2015-12-12 05:38:55 +00004067CatchSwitchInst *CatchSwitchInst::cloneImpl() const {
4068 return new CatchSwitchInst(*this);
4069}
4070
4071FuncletPadInst *FuncletPadInst::cloneImpl() const {
4072 return new (getNumOperands()) FuncletPadInst(*this);
David Majnemer654e1302015-07-31 17:58:14 +00004073}
4074
Pete Cooper75403d72015-06-24 20:22:23 +00004075UnreachableInst *UnreachableInst::cloneImpl() const {
Nick Lewycky42fb7452009-09-27 07:38:41 +00004076 LLVMContext &Context = getContext();
Devang Patel11cf3f42009-10-27 22:16:29 +00004077 return new UnreachableInst(Context);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004078}