blob: 023a0b178a1451916d8404d0386d3c711925ee77 [file] [log] [blame]
Eugene Zelenkod761e2c2017-05-15 21:57:41 +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 Carruth6bda14b2017-06-06 11:49:48 +000015#include "llvm/IR/Instructions.h"
Devang Pateladd58652009-09-23 18:32:25 +000016#include "LLVMContextImpl.h"
Eugene Zelenkod761e2c2017-05-15 21:57:41 +000017#include "llvm/ADT/None.h"
18#include "llvm/ADT/SmallVector.h"
19#include "llvm/ADT/Twine.h"
20#include "llvm/IR/Attributes.h"
21#include "llvm/IR/BasicBlock.h"
Chandler Carruth219b89b2014-03-04 11:01:28 +000022#include "llvm/IR/CallSite.h"
Eugene Zelenkod761e2c2017-05-15 21:57:41 +000023#include "llvm/IR/Constant.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000024#include "llvm/IR/Constants.h"
25#include "llvm/IR/DataLayout.h"
26#include "llvm/IR/DerivedTypes.h"
27#include "llvm/IR/Function.h"
Eugene Zelenkod761e2c2017-05-15 21:57:41 +000028#include "llvm/IR/InstrTypes.h"
29#include "llvm/IR/Instruction.h"
Eugene Zelenkod761e2c2017-05-15 21:57:41 +000030#include "llvm/IR/LLVMContext.h"
31#include "llvm/IR/Metadata.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000032#include "llvm/IR/Module.h"
33#include "llvm/IR/Operator.h"
Eugene Zelenkod761e2c2017-05-15 21:57:41 +000034#include "llvm/IR/Type.h"
35#include "llvm/IR/Value.h"
36#include "llvm/Support/AtomicOrdering.h"
37#include "llvm/Support/Casting.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000038#include "llvm/Support/ErrorHandling.h"
Christopher Lamb84485702007-04-22 19:24:39 +000039#include "llvm/Support/MathExtras.h"
Eugene Zelenkod761e2c2017-05-15 21:57:41 +000040#include <algorithm>
41#include <cassert>
42#include <cstdint>
43#include <vector>
44
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000045using namespace llvm;
46
Chris Lattner3e13b8c2008-01-02 23:42:30 +000047//===----------------------------------------------------------------------===//
48// CallSite Class
49//===----------------------------------------------------------------------===//
50
Gabor Greifa2fbc0a2010-03-24 13:21:49 +000051User::op_iterator CallSite::getCallee() const {
52 Instruction *II(getInstruction());
53 return isCall()
Gabor Greif638c8232010-08-05 21:25:49 +000054 ? cast<CallInst>(II)->op_end() - 1 // Skip Callee
Gabor Greif6d673952010-07-16 09:38:02 +000055 : cast<InvokeInst>(II)->op_end() - 3; // Skip BB, BB, Callee
Gabor Greifa2fbc0a2010-03-24 13:21:49 +000056}
57
Gordon Henriksen14a55692007-12-10 02:14:30 +000058//===----------------------------------------------------------------------===//
59// TerminatorInst Class
60//===----------------------------------------------------------------------===//
61
Reid Kleckner45a13e12017-05-11 21:26:55 +000062unsigned TerminatorInst::getNumSuccessors() const {
63 switch (getOpcode()) {
64#define HANDLE_TERM_INST(N, OPC, CLASS) \
65 case Instruction::OPC: \
Craig Topperc1993fa2017-06-08 23:23:08 +000066 return static_cast<const CLASS *>(this)->getNumSuccessors();
Reid Kleckner45a13e12017-05-11 21:26:55 +000067#include "llvm/IR/Instruction.def"
68 default:
69 break;
70 }
71 llvm_unreachable("not a terminator");
72}
73
74BasicBlock *TerminatorInst::getSuccessor(unsigned idx) const {
75 switch (getOpcode()) {
76#define HANDLE_TERM_INST(N, OPC, CLASS) \
77 case Instruction::OPC: \
Craig Topperc1993fa2017-06-08 23:23:08 +000078 return static_cast<const CLASS *>(this)->getSuccessor(idx);
Reid Kleckner45a13e12017-05-11 21:26:55 +000079#include "llvm/IR/Instruction.def"
80 default:
81 break;
82 }
83 llvm_unreachable("not a terminator");
84}
85
86void TerminatorInst::setSuccessor(unsigned idx, BasicBlock *B) {
87 switch (getOpcode()) {
88#define HANDLE_TERM_INST(N, OPC, CLASS) \
89 case Instruction::OPC: \
Craig Topperc1993fa2017-06-08 23:23:08 +000090 return static_cast<CLASS *>(this)->setSuccessor(idx, B);
Reid Kleckner45a13e12017-05-11 21:26:55 +000091#include "llvm/IR/Instruction.def"
92 default:
93 break;
94 }
95 llvm_unreachable("not a terminator");
96}
97
Gabor Greiff6caff662008-05-10 08:32:32 +000098//===----------------------------------------------------------------------===//
Chris Lattner88107952008-12-29 00:12:50 +000099// SelectInst Class
100//===----------------------------------------------------------------------===//
101
102/// areInvalidOperands - Return a string if the specified operands are invalid
103/// for a select operation, otherwise return null.
104const char *SelectInst::areInvalidOperands(Value *Op0, Value *Op1, Value *Op2) {
105 if (Op1->getType() != Op2->getType())
106 return "both values to select must have same type";
David Majnemerb611e3f2015-08-14 05:09:07 +0000107
108 if (Op1->getType()->isTokenTy())
109 return "select values cannot have token type";
110
Chris Lattner229907c2011-07-18 04:54:35 +0000111 if (VectorType *VT = dyn_cast<VectorType>(Op0->getType())) {
Chris Lattner88107952008-12-29 00:12:50 +0000112 // Vector select.
Owen Anderson55f1c092009-08-13 21:58:54 +0000113 if (VT->getElementType() != Type::getInt1Ty(Op0->getContext()))
Chris Lattner88107952008-12-29 00:12:50 +0000114 return "vector select condition element type must be i1";
Chris Lattner229907c2011-07-18 04:54:35 +0000115 VectorType *ET = dyn_cast<VectorType>(Op1->getType());
Craig Topperc6207612014-04-09 06:08:46 +0000116 if (!ET)
Chris Lattner88107952008-12-29 00:12:50 +0000117 return "selected values for vector select must be vectors";
118 if (ET->getNumElements() != VT->getNumElements())
119 return "vector select requires selected vectors to have "
120 "the same vector length as select condition";
Owen Anderson55f1c092009-08-13 21:58:54 +0000121 } else if (Op0->getType() != Type::getInt1Ty(Op0->getContext())) {
Chris Lattner88107952008-12-29 00:12:50 +0000122 return "select condition must be i1 or <n x i1>";
123 }
Craig Topperc6207612014-04-09 06:08:46 +0000124 return nullptr;
Chris Lattner88107952008-12-29 00:12:50 +0000125}
126
Chris Lattner88107952008-12-29 00:12:50 +0000127//===----------------------------------------------------------------------===//
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000128// PHINode Class
129//===----------------------------------------------------------------------===//
130
131PHINode::PHINode(const PHINode &PN)
Pete Cooper3fc30402015-06-10 22:38:46 +0000132 : Instruction(PN.getType(), Instruction::PHI, nullptr, PN.getNumOperands()),
133 ReservedSpace(PN.getNumOperands()) {
134 allocHungoffUses(PN.getNumOperands());
Jay Foad61ea0e42011-06-23 09:09:15 +0000135 std::copy(PN.op_begin(), PN.op_end(), op_begin());
136 std::copy(PN.block_begin(), PN.block_end(), block_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000137 SubclassOptionalData = PN.SubclassOptionalData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000138}
139
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000140// removeIncomingValue - Remove an incoming value. This is useful if a
141// predecessor basic block is deleted.
142Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
Jay Foad61ea0e42011-06-23 09:09:15 +0000143 Value *Removed = getIncomingValue(Idx);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000144
145 // Move everything after this operand down.
146 //
147 // FIXME: we could just swap with the end of the list, then erase. However,
Jay Foad61ea0e42011-06-23 09:09:15 +0000148 // clients might not expect this to happen. The code as it is thrashes the
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000149 // use/def lists, which is kinda lame.
Jay Foad61ea0e42011-06-23 09:09:15 +0000150 std::copy(op_begin() + Idx + 1, op_end(), op_begin() + Idx);
151 std::copy(block_begin() + Idx + 1, block_end(), block_begin() + Idx);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000152
153 // Nuke the last value.
Craig Topperc6207612014-04-09 06:08:46 +0000154 Op<-1>().set(nullptr);
Pete Cooperb4eede22015-06-12 17:48:10 +0000155 setNumHungOffUseOperands(getNumOperands() - 1);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000156
157 // If the PHI node is dead, because it has zero entries, nuke it now.
Jay Foad61ea0e42011-06-23 09:09:15 +0000158 if (getNumOperands() == 0 && DeletePHIIfEmpty) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000159 // If anyone is using this PHI, make them use a dummy value instead...
Owen Andersonb292b8c2009-07-30 23:03:37 +0000160 replaceAllUsesWith(UndefValue::get(getType()));
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000161 eraseFromParent();
162 }
163 return Removed;
164}
165
Jay Foade98f29d2011-04-01 08:00:58 +0000166/// growOperands - grow operands - This grows the operand list in response
167/// to a push_back style of operation. This grows the number of ops by 1.5
168/// times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000169///
Jay Foade98f29d2011-04-01 08:00:58 +0000170void PHINode::growOperands() {
Gabor Greiff6caff662008-05-10 08:32:32 +0000171 unsigned e = getNumOperands();
Jay Foad61ea0e42011-06-23 09:09:15 +0000172 unsigned NumOps = e + e / 2;
173 if (NumOps < 2) NumOps = 2; // 2 op PHI nodes are VERY common.
174
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000175 ReservedSpace = NumOps;
Pete Cooper93f9ff52015-06-10 22:38:41 +0000176 growHungoffUses(ReservedSpace, /* IsPhi */ true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000177}
178
Nate Begemanb3923212005-08-04 23:24:19 +0000179/// hasConstantValue - If the specified PHI node always merges together the same
180/// value, return the value, otherwise return null.
Duncan Sands7412f6e2010-11-17 04:30:22 +0000181Value *PHINode::hasConstantValue() const {
182 // Exploit the fact that phi nodes always have at least one entry.
183 Value *ConstantValue = getIncomingValue(0);
184 for (unsigned i = 1, e = getNumIncomingValues(); i != e; ++i)
Nuno Lopes90c76df2012-07-03 17:10:28 +0000185 if (getIncomingValue(i) != ConstantValue && getIncomingValue(i) != this) {
186 if (ConstantValue != this)
Craig Topperc6207612014-04-09 06:08:46 +0000187 return nullptr; // Incoming values not all the same.
Nuno Lopes90c76df2012-07-03 17:10:28 +0000188 // The case where the first value is this PHI.
189 ConstantValue = getIncomingValue(i);
190 }
Nuno Lopes0d44a502012-07-03 21:15:40 +0000191 if (ConstantValue == this)
192 return UndefValue::get(getType());
Duncan Sands7412f6e2010-11-17 04:30:22 +0000193 return ConstantValue;
Nate Begemanb3923212005-08-04 23:24:19 +0000194}
195
Nicolai Haehnle13d90f32016-04-14 17:42:47 +0000196/// hasConstantOrUndefValue - Whether the specified PHI node always merges
197/// together the same value, assuming that undefs result in the same value as
198/// non-undefs.
199/// Unlike \ref hasConstantValue, this does not return a value because the
200/// unique non-undef incoming value need not dominate the PHI node.
201bool PHINode::hasConstantOrUndefValue() const {
202 Value *ConstantValue = nullptr;
203 for (unsigned i = 0, e = getNumIncomingValues(); i != e; ++i) {
204 Value *Incoming = getIncomingValue(i);
205 if (Incoming != this && !isa<UndefValue>(Incoming)) {
206 if (ConstantValue && ConstantValue != Incoming)
207 return false;
208 ConstantValue = Incoming;
209 }
210 }
211 return true;
212}
213
Bill Wendlingfae14752011-08-12 20:24:12 +0000214//===----------------------------------------------------------------------===//
215// LandingPadInst Implementation
216//===----------------------------------------------------------------------===//
217
David Majnemer7fddecc2015-06-17 20:52:32 +0000218LandingPadInst::LandingPadInst(Type *RetTy, unsigned NumReservedValues,
219 const Twine &NameStr, Instruction *InsertBefore)
220 : Instruction(RetTy, Instruction::LandingPad, nullptr, 0, InsertBefore) {
221 init(NumReservedValues, NameStr);
Bill Wendlingfae14752011-08-12 20:24:12 +0000222}
223
David Majnemer7fddecc2015-06-17 20:52:32 +0000224LandingPadInst::LandingPadInst(Type *RetTy, unsigned NumReservedValues,
225 const Twine &NameStr, BasicBlock *InsertAtEnd)
226 : Instruction(RetTy, Instruction::LandingPad, nullptr, 0, InsertAtEnd) {
227 init(NumReservedValues, NameStr);
Bill Wendlingfae14752011-08-12 20:24:12 +0000228}
229
230LandingPadInst::LandingPadInst(const LandingPadInst &LP)
Pete Cooper3fc30402015-06-10 22:38:46 +0000231 : Instruction(LP.getType(), Instruction::LandingPad, nullptr,
232 LP.getNumOperands()),
233 ReservedSpace(LP.getNumOperands()) {
234 allocHungoffUses(LP.getNumOperands());
Pete Cooper74510a42015-06-12 17:48:05 +0000235 Use *OL = getOperandList();
236 const Use *InOL = LP.getOperandList();
Bill Wendlingfae14752011-08-12 20:24:12 +0000237 for (unsigned I = 0, E = ReservedSpace; I != E; ++I)
238 OL[I] = InOL[I];
239
240 setCleanup(LP.isCleanup());
241}
242
David Majnemer7fddecc2015-06-17 20:52:32 +0000243LandingPadInst *LandingPadInst::Create(Type *RetTy, unsigned NumReservedClauses,
Bill Wendlingfae14752011-08-12 20:24:12 +0000244 const Twine &NameStr,
245 Instruction *InsertBefore) {
David Majnemer7fddecc2015-06-17 20:52:32 +0000246 return new LandingPadInst(RetTy, NumReservedClauses, NameStr, InsertBefore);
Bill Wendlingfae14752011-08-12 20:24:12 +0000247}
248
David Majnemer7fddecc2015-06-17 20:52:32 +0000249LandingPadInst *LandingPadInst::Create(Type *RetTy, unsigned NumReservedClauses,
Bill Wendlingfae14752011-08-12 20:24:12 +0000250 const Twine &NameStr,
251 BasicBlock *InsertAtEnd) {
David Majnemer7fddecc2015-06-17 20:52:32 +0000252 return new LandingPadInst(RetTy, NumReservedClauses, NameStr, InsertAtEnd);
Bill Wendlingfae14752011-08-12 20:24:12 +0000253}
254
David Majnemer7fddecc2015-06-17 20:52:32 +0000255void LandingPadInst::init(unsigned NumReservedValues, const Twine &NameStr) {
Bill Wendlingfae14752011-08-12 20:24:12 +0000256 ReservedSpace = NumReservedValues;
David Majnemer7fddecc2015-06-17 20:52:32 +0000257 setNumHungOffUseOperands(0);
Pete Cooper3fc30402015-06-10 22:38:46 +0000258 allocHungoffUses(ReservedSpace);
Bill Wendlingfae14752011-08-12 20:24:12 +0000259 setName(NameStr);
260 setCleanup(false);
261}
262
263/// growOperands - grow operands - This grows the operand list in response to a
264/// push_back style of operation. This grows the number of ops by 2 times.
265void LandingPadInst::growOperands(unsigned Size) {
266 unsigned e = getNumOperands();
267 if (ReservedSpace >= e + Size) return;
David Majnemer7fddecc2015-06-17 20:52:32 +0000268 ReservedSpace = (std::max(e, 1U) + Size / 2) * 2;
Pete Cooper93f9ff52015-06-10 22:38:41 +0000269 growHungoffUses(ReservedSpace);
Bill Wendlingfae14752011-08-12 20:24:12 +0000270}
271
Rafael Espindola4dc5dfc2014-06-04 18:51:31 +0000272void LandingPadInst::addClause(Constant *Val) {
Bill Wendlingfae14752011-08-12 20:24:12 +0000273 unsigned OpNo = getNumOperands();
274 growOperands(1);
275 assert(OpNo < ReservedSpace && "Growing didn't work!");
Pete Cooperb4eede22015-06-12 17:48:10 +0000276 setNumHungOffUseOperands(getNumOperands() + 1);
Pete Cooper74510a42015-06-12 17:48:05 +0000277 getOperandList()[OpNo] = Val;
Bill Wendlingfae14752011-08-12 20:24:12 +0000278}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000279
280//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000281// CallInst Implementation
282//===----------------------------------------------------------------------===//
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) {
Reid Kleckner5fbdd172017-05-31 19:23:09 +0000396 assert(ArgNo < getNumArgOperands() && "Out of bounds");
397 AttributeList PAL = getAttributes();
398 PAL = PAL.addParamAttribute(getContext(), ArgNo, Kind);
399 setAttributes(PAL);
400}
401
402void CallInst::addParamAttr(unsigned ArgNo, Attribute Attr) {
403 assert(ArgNo < getNumArgOperands() && "Out of bounds");
404 AttributeList PAL = getAttributes();
405 PAL = PAL.addParamAttribute(getContext(), ArgNo, Attr);
406 setAttributes(PAL);
Reid Klecknera0b45f42017-05-03 18:17:31 +0000407}
408
Amaury Sechet392638d2016-06-14 20:27:35 +0000409void CallInst::removeAttribute(unsigned i, Attribute::AttrKind Kind) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000410 AttributeList PAL = getAttributes();
Amaury Sechet392638d2016-06-14 20:27:35 +0000411 PAL = PAL.removeAttribute(getContext(), i, Kind);
Amaury Sechet1a0e0972016-04-21 21:29:10 +0000412 setAttributes(PAL);
413}
414
Amaury Sechet6100adf2016-06-15 17:50:39 +0000415void CallInst::removeAttribute(unsigned i, StringRef Kind) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000416 AttributeList PAL = getAttributes();
Amaury Sechet6100adf2016-06-15 17:50:39 +0000417 PAL = PAL.removeAttribute(getContext(), i, Kind);
418 setAttributes(PAL);
419}
420
Reid Klecknera0b45f42017-05-03 18:17:31 +0000421void CallInst::removeParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) {
Reid Kleckner5fbdd172017-05-31 19:23:09 +0000422 assert(ArgNo < getNumArgOperands() && "Out of bounds");
423 AttributeList PAL = getAttributes();
424 PAL = PAL.removeParamAttribute(getContext(), ArgNo, Kind);
425 setAttributes(PAL);
426}
427
428void CallInst::removeParamAttr(unsigned ArgNo, StringRef Kind) {
429 assert(ArgNo < getNumArgOperands() && "Out of bounds");
430 AttributeList PAL = getAttributes();
431 PAL = PAL.removeParamAttribute(getContext(), ArgNo, Kind);
432 setAttributes(PAL);
Reid Klecknera0b45f42017-05-03 18:17:31 +0000433}
434
Ramkumar Ramachandra8fcb4982015-02-14 19:37:54 +0000435void CallInst::addDereferenceableAttr(unsigned i, uint64_t Bytes) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000436 AttributeList PAL = getAttributes();
Ramkumar Ramachandra8fcb4982015-02-14 19:37:54 +0000437 PAL = PAL.addDereferenceableAttr(getContext(), i, Bytes);
438 setAttributes(PAL);
439}
440
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000441void CallInst::addDereferenceableOrNullAttr(unsigned i, uint64_t Bytes) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000442 AttributeList PAL = getAttributes();
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000443 PAL = PAL.addDereferenceableOrNullAttr(getContext(), i, Bytes);
444 setAttributes(PAL);
445}
446
Reid Klecknerfb502d22017-04-14 20:19:02 +0000447bool CallInst::hasRetAttr(Attribute::AttrKind Kind) const {
448 if (Attrs.hasAttribute(AttributeList::ReturnIndex, Kind))
449 return true;
Sanjoy Dasb11b4402015-11-04 20:33:45 +0000450
Reid Klecknerfb502d22017-04-14 20:19:02 +0000451 // Look at the callee, if available.
452 if (const Function *F = getCalledFunction())
453 return F->getAttributes().hasAttribute(AttributeList::ReturnIndex, Kind);
454 return false;
455}
456
457bool CallInst::paramHasAttr(unsigned i, Attribute::AttrKind Kind) const {
458 assert(i < getNumArgOperands() && "Param index out of bounds!");
459
460 if (Attrs.hasParamAttribute(i, Kind))
Bill Wendling8baa61d2012-10-03 17:54:26 +0000461 return true;
462 if (const Function *F = getCalledFunction())
Reid Klecknerfb502d22017-04-14 20:19:02 +0000463 return F->getAttributes().hasParamAttribute(i, Kind);
Bill Wendlingdaf8e382012-10-04 07:18:12 +0000464 return false;
465}
466
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000467bool CallInst::dataOperandHasImpliedAttr(unsigned i,
Amaury Sechet392638d2016-06-14 20:27:35 +0000468 Attribute::AttrKind Kind) const {
Sanjoy Das776e4a72015-11-05 01:53:26 +0000469 // There are getNumOperands() - 1 data operands. The last operand is the
470 // callee.
471 assert(i < getNumOperands() && "Data operand index out of bounds!");
472
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000473 // The attribute A can either be directly specified, if the operand in
474 // question is a call argument; or be indirectly implied by the kind of its
475 // containing operand bundle, if the operand is a bundle operand.
476
Reid Kleckner545aa4f2017-05-23 17:03:28 +0000477 if (i == AttributeList::ReturnIndex)
Reid Kleckner8bf67fe2017-05-23 17:01:48 +0000478 return hasRetAttr(Kind);
479
Reid Klecknerfb502d22017-04-14 20:19:02 +0000480 // FIXME: Avoid these i - 1 calculations and update the API to use zero-based
481 // indices.
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000482 if (i < (getNumArgOperands() + 1))
Reid Klecknerfb502d22017-04-14 20:19:02 +0000483 return paramHasAttr(i - 1, Kind);
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000484
485 assert(hasOperandBundles() && i >= (getBundleOperandsStartIndex() + 1) &&
486 "Must be either a call argument or an operand bundle!");
Amaury Sechet392638d2016-06-14 20:27:35 +0000487 return bundleOperandHasAttr(i - 1, Kind);
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000488}
489
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000490/// IsConstantOne - Return true only if val is constant int 1
491static bool IsConstantOne(Value *val) {
Reid Kleckner971c3ea2014-11-13 22:55:19 +0000492 assert(val && "IsConstantOne does not work with nullptr val");
Matt Arsenault69417852014-09-15 17:56:51 +0000493 const ConstantInt *CVal = dyn_cast<ConstantInt>(val);
494 return CVal && CVal->isOne();
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000495}
496
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000497static Instruction *createMalloc(Instruction *InsertBefore,
Chris Lattner229907c2011-07-18 04:54:35 +0000498 BasicBlock *InsertAtEnd, Type *IntPtrTy,
David Majnemerfadc6db2016-04-29 08:07:22 +0000499 Type *AllocTy, Value *AllocSize,
500 Value *ArraySize,
501 ArrayRef<OperandBundleDef> OpB,
502 Function *MallocF, const Twine &Name) {
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000503 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
Victor Hernandez788eaab2009-09-18 19:20:02 +0000504 "createMalloc needs either InsertBefore or InsertAtEnd");
505
506 // malloc(type) becomes:
507 // bitcast (i8* malloc(typeSize)) to type*
508 // malloc(type, arraySize) becomes:
Ana Pazosb3596022016-02-03 21:34:39 +0000509 // bitcast (i8* malloc(typeSize*arraySize)) to type*
Victor Hernandezf3db9152009-11-07 00:16:28 +0000510 if (!ArraySize)
511 ArraySize = ConstantInt::get(IntPtrTy, 1);
512 else if (ArraySize->getType() != IntPtrTy) {
513 if (InsertBefore)
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000514 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
515 "", InsertBefore);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000516 else
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000517 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
518 "", InsertAtEnd);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000519 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000520
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000521 if (!IsConstantOne(ArraySize)) {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000522 if (IsConstantOne(AllocSize)) {
523 AllocSize = ArraySize; // Operand * 1 = Operand
524 } else if (Constant *CO = dyn_cast<Constant>(ArraySize)) {
525 Constant *Scale = ConstantExpr::getIntegerCast(CO, IntPtrTy,
526 false /*ZExt*/);
527 // Malloc arg is constant product of type size and array size
528 AllocSize = ConstantExpr::getMul(Scale, cast<Constant>(AllocSize));
529 } else {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000530 // Multiply type size by the array size...
531 if (InsertBefore)
Victor Hernandez788eaab2009-09-18 19:20:02 +0000532 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
533 "mallocsize", InsertBefore);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000534 else
Victor Hernandez788eaab2009-09-18 19:20:02 +0000535 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
536 "mallocsize", InsertAtEnd);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000537 }
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000538 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000539
Victor Hernandez788eaab2009-09-18 19:20:02 +0000540 assert(AllocSize->getType() == IntPtrTy && "malloc arg is wrong size");
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000541 // Create the call to Malloc.
Ana Pazosb3596022016-02-03 21:34:39 +0000542 BasicBlock *BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
543 Module *M = BB->getParent()->getParent();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000544 Type *BPTy = Type::getInt8PtrTy(BB->getContext());
Victor Hernandezbb336a12009-11-10 19:53:28 +0000545 Value *MallocFunc = MallocF;
546 if (!MallocFunc)
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000547 // prototype malloc as "void *malloc(size_t)"
Serge Guelton59a2d7b2017-04-11 15:01:18 +0000548 MallocFunc = M->getOrInsertFunction("malloc", BPTy, IntPtrTy);
Chris Lattner229907c2011-07-18 04:54:35 +0000549 PointerType *AllocPtrType = PointerType::getUnqual(AllocTy);
Craig Topperc6207612014-04-09 06:08:46 +0000550 CallInst *MCall = nullptr;
551 Instruction *Result = nullptr;
Victor Hernandez788eaab2009-09-18 19:20:02 +0000552 if (InsertBefore) {
David Majnemerfadc6db2016-04-29 08:07:22 +0000553 MCall = CallInst::Create(MallocFunc, AllocSize, OpB, "malloccall",
554 InsertBefore);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000555 Result = MCall;
556 if (Result->getType() != AllocPtrType)
557 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000558 Result = new BitCastInst(MCall, AllocPtrType, Name, InsertBefore);
Victor Hernandez788eaab2009-09-18 19:20:02 +0000559 } else {
David Majnemerfadc6db2016-04-29 08:07:22 +0000560 MCall = CallInst::Create(MallocFunc, AllocSize, OpB, "malloccall");
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000561 Result = MCall;
562 if (Result->getType() != AllocPtrType) {
563 InsertAtEnd->getInstList().push_back(MCall);
564 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000565 Result = new BitCastInst(MCall, AllocPtrType, Name);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000566 }
Victor Hernandez788eaab2009-09-18 19:20:02 +0000567 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000568 MCall->setTailCall();
Victor Hernandezbb336a12009-11-10 19:53:28 +0000569 if (Function *F = dyn_cast<Function>(MallocFunc)) {
570 MCall->setCallingConv(F->getCallingConv());
Reid Klecknera0b45f42017-05-03 18:17:31 +0000571 if (!F->returnDoesNotAlias())
572 F->setReturnDoesNotAlias();
Victor Hernandezbb336a12009-11-10 19:53:28 +0000573 }
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000574 assert(!MCall->getType()->isVoidTy() && "Malloc has void return type");
Victor Hernandez788eaab2009-09-18 19:20:02 +0000575
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000576 return Result;
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000577}
578
579/// CreateMalloc - Generate the IR for a call to malloc:
580/// 1. Compute the malloc call's argument as the specified type's size,
581/// possibly multiplied by the array size if the array size is not
582/// constant 1.
583/// 2. Call malloc with that argument.
584/// 3. Bitcast the result of the malloc call to the specified type.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000585Instruction *CallInst::CreateMalloc(Instruction *InsertBefore,
Chris Lattner229907c2011-07-18 04:54:35 +0000586 Type *IntPtrTy, Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000587 Value *AllocSize, Value *ArraySize,
Ana Pazosb3596022016-02-03 21:34:39 +0000588 Function *MallocF,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000589 const Twine &Name) {
Craig Topperc6207612014-04-09 06:08:46 +0000590 return createMalloc(InsertBefore, nullptr, IntPtrTy, AllocTy, AllocSize,
David Majnemerfadc6db2016-04-29 08:07:22 +0000591 ArraySize, None, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000592}
David Majnemerfadc6db2016-04-29 08:07:22 +0000593Instruction *CallInst::CreateMalloc(Instruction *InsertBefore,
594 Type *IntPtrTy, Type *AllocTy,
595 Value *AllocSize, Value *ArraySize,
596 ArrayRef<OperandBundleDef> OpB,
597 Function *MallocF,
598 const Twine &Name) {
599 return createMalloc(InsertBefore, nullptr, IntPtrTy, AllocTy, AllocSize,
600 ArraySize, OpB, MallocF, Name);
601}
602
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000603/// CreateMalloc - Generate the IR for a call to malloc:
604/// 1. Compute the malloc call's argument as the specified type's size,
605/// possibly multiplied by the array size if the array size is not
606/// constant 1.
607/// 2. Call malloc with that argument.
608/// 3. Bitcast the result of the malloc call to the specified type.
609/// Note: This function does not add the bitcast to the basic block, that is the
610/// responsibility of the caller.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000611Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd,
Chris Lattner229907c2011-07-18 04:54:35 +0000612 Type *IntPtrTy, Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000613 Value *AllocSize, Value *ArraySize,
614 Function *MallocF, const Twine &Name) {
Craig Topperc6207612014-04-09 06:08:46 +0000615 return createMalloc(nullptr, InsertAtEnd, IntPtrTy, AllocTy, AllocSize,
David Majnemerfadc6db2016-04-29 08:07:22 +0000616 ArraySize, None, MallocF, Name);
617}
618Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd,
619 Type *IntPtrTy, Type *AllocTy,
620 Value *AllocSize, Value *ArraySize,
621 ArrayRef<OperandBundleDef> OpB,
622 Function *MallocF, const Twine &Name) {
623 return createMalloc(nullptr, InsertAtEnd, IntPtrTy, AllocTy, AllocSize,
624 ArraySize, OpB, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000625}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000626
David Majnemerfadc6db2016-04-29 08:07:22 +0000627static Instruction *createFree(Value *Source,
628 ArrayRef<OperandBundleDef> Bundles,
629 Instruction *InsertBefore,
Victor Hernandeze2971492009-10-24 04:23:03 +0000630 BasicBlock *InsertAtEnd) {
631 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
632 "createFree needs either InsertBefore or InsertAtEnd");
Duncan Sands19d0b472010-02-16 11:11:14 +0000633 assert(Source->getType()->isPointerTy() &&
Victor Hernandeze2971492009-10-24 04:23:03 +0000634 "Can not free something of nonpointer type!");
635
Ana Pazosb3596022016-02-03 21:34:39 +0000636 BasicBlock *BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
637 Module *M = BB->getParent()->getParent();
Victor Hernandeze2971492009-10-24 04:23:03 +0000638
Chris Lattner229907c2011-07-18 04:54:35 +0000639 Type *VoidTy = Type::getVoidTy(M->getContext());
640 Type *IntPtrTy = Type::getInt8PtrTy(M->getContext());
Victor Hernandeze2971492009-10-24 04:23:03 +0000641 // prototype free as "void free(void*)"
Serge Guelton59a2d7b2017-04-11 15:01:18 +0000642 Value *FreeFunc = M->getOrInsertFunction("free", VoidTy, IntPtrTy);
Ana Pazosb3596022016-02-03 21:34:39 +0000643 CallInst *Result = nullptr;
Victor Hernandeze2971492009-10-24 04:23:03 +0000644 Value *PtrCast = Source;
645 if (InsertBefore) {
646 if (Source->getType() != IntPtrTy)
647 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertBefore);
David Majnemerfadc6db2016-04-29 08:07:22 +0000648 Result = CallInst::Create(FreeFunc, PtrCast, Bundles, "", InsertBefore);
Victor Hernandeze2971492009-10-24 04:23:03 +0000649 } else {
650 if (Source->getType() != IntPtrTy)
651 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertAtEnd);
David Majnemerfadc6db2016-04-29 08:07:22 +0000652 Result = CallInst::Create(FreeFunc, PtrCast, Bundles, "");
Victor Hernandeze2971492009-10-24 04:23:03 +0000653 }
654 Result->setTailCall();
Chris Lattner2156c222009-11-09 07:12:01 +0000655 if (Function *F = dyn_cast<Function>(FreeFunc))
656 Result->setCallingConv(F->getCallingConv());
Victor Hernandeze2971492009-10-24 04:23:03 +0000657
658 return Result;
659}
660
661/// CreateFree - Generate the IR for a call to the builtin free function.
Ana Pazosb3596022016-02-03 21:34:39 +0000662Instruction *CallInst::CreateFree(Value *Source, Instruction *InsertBefore) {
David Majnemerfadc6db2016-04-29 08:07:22 +0000663 return createFree(Source, None, InsertBefore, nullptr);
664}
665Instruction *CallInst::CreateFree(Value *Source,
666 ArrayRef<OperandBundleDef> Bundles,
667 Instruction *InsertBefore) {
668 return createFree(Source, Bundles, InsertBefore, nullptr);
Victor Hernandeze2971492009-10-24 04:23:03 +0000669}
670
671/// CreateFree - Generate the IR for a call to the builtin free function.
672/// Note: This function does not add the call to the basic block, that is the
673/// responsibility of the caller.
Ana Pazosb3596022016-02-03 21:34:39 +0000674Instruction *CallInst::CreateFree(Value *Source, BasicBlock *InsertAtEnd) {
David Majnemerfadc6db2016-04-29 08:07:22 +0000675 Instruction *FreeCall = createFree(Source, None, nullptr, InsertAtEnd);
676 assert(FreeCall && "CreateFree did not create a CallInst");
677 return FreeCall;
678}
679Instruction *CallInst::CreateFree(Value *Source,
680 ArrayRef<OperandBundleDef> Bundles,
681 BasicBlock *InsertAtEnd) {
682 Instruction *FreeCall = createFree(Source, Bundles, nullptr, InsertAtEnd);
Victor Hernandeze2971492009-10-24 04:23:03 +0000683 assert(FreeCall && "CreateFree did not create a CallInst");
684 return FreeCall;
685}
686
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000687//===----------------------------------------------------------------------===//
688// InvokeInst Implementation
689//===----------------------------------------------------------------------===//
690
David Blaikie3e807092015-05-13 18:35:26 +0000691void InvokeInst::init(FunctionType *FTy, Value *Fn, BasicBlock *IfNormal,
692 BasicBlock *IfException, ArrayRef<Value *> Args,
Sanjoy Das9303c242015-09-24 19:14:18 +0000693 ArrayRef<OperandBundleDef> Bundles,
David Blaikie3e807092015-05-13 18:35:26 +0000694 const Twine &NameStr) {
695 this->FTy = FTy;
David Blaikie348de692015-04-23 21:36:23 +0000696
Sanjoy Das9303c242015-09-24 19:14:18 +0000697 assert(getNumOperands() == 3 + Args.size() + CountBundleInputs(Bundles) &&
698 "NumOperands not set up?");
Gabor Greifa2fbc0a2010-03-24 13:21:49 +0000699 Op<-3>() = Fn;
700 Op<-2>() = IfNormal;
701 Op<-1>() = IfException;
Jay Foad5bd375a2011-07-15 08:37:34 +0000702
703#ifndef NDEBUG
Jay Foad5bd375a2011-07-15 08:37:34 +0000704 assert(((Args.size() == FTy->getNumParams()) ||
705 (FTy->isVarArg() && Args.size() > FTy->getNumParams())) &&
Gabor Greif668d7002010-03-23 13:45:54 +0000706 "Invoking a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000707
Jay Foad5bd375a2011-07-15 08:37:34 +0000708 for (unsigned i = 0, e = Args.size(); i != e; i++)
Chris Lattner667a0562006-05-03 00:48:22 +0000709 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000710 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000711 "Invoking a function with a bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000712#endif
713
714 std::copy(Args.begin(), Args.end(), op_begin());
Sanjoy Das9303c242015-09-24 19:14:18 +0000715
716 auto It = populateBundleOperandInfos(Bundles, Args.size());
717 (void)It;
718 assert(It + 3 == op_end() && "Should add up!");
719
Jay Foad5bd375a2011-07-15 08:37:34 +0000720 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000721}
722
Misha Brukmanb1c93172005-04-21 23:48:37 +0000723InvokeInst::InvokeInst(const InvokeInst &II)
David Blaikie348de692015-04-23 21:36:23 +0000724 : TerminatorInst(II.getType(), Instruction::Invoke,
725 OperandTraits<InvokeInst>::op_end(this) -
726 II.getNumOperands(),
727 II.getNumOperands()),
Reid Klecknerb5180542017-03-21 16:57:19 +0000728 Attrs(II.Attrs), FTy(II.FTy) {
Chris Lattnerb9c86512009-12-29 02:14:09 +0000729 setCallingConv(II.getCallingConv());
Jay Foad5bd375a2011-07-15 08:37:34 +0000730 std::copy(II.op_begin(), II.op_end(), op_begin());
Sanjoy Das9303c242015-09-24 19:14:18 +0000731 std::copy(II.bundle_op_info_begin(), II.bundle_op_info_end(),
732 bundle_op_info_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000733 SubclassOptionalData = II.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000734}
735
Sanjoy Das2d161452015-11-18 06:23:38 +0000736InvokeInst *InvokeInst::Create(InvokeInst *II, ArrayRef<OperandBundleDef> OpB,
737 Instruction *InsertPt) {
Sanjoy Dasccd14562015-12-10 06:39:02 +0000738 std::vector<Value *> Args(II->arg_begin(), II->arg_end());
Sanjoy Das2d161452015-11-18 06:23:38 +0000739
740 auto *NewII = InvokeInst::Create(II->getCalledValue(), II->getNormalDest(),
741 II->getUnwindDest(), Args, OpB,
742 II->getName(), InsertPt);
743 NewII->setCallingConv(II->getCallingConv());
744 NewII->SubclassOptionalData = II->SubclassOptionalData;
Sanjoy Dasb8dced52015-12-09 01:01:28 +0000745 NewII->setAttributes(II->getAttributes());
Joseph Tremouletbba70e42016-01-14 06:21:42 +0000746 NewII->setDebugLoc(II->getDebugLoc());
Sanjoy Das2d161452015-11-18 06:23:38 +0000747 return NewII;
748}
749
Hal Finkele87ad542016-07-10 23:01:32 +0000750Value *InvokeInst::getReturnedArgOperand() const {
751 unsigned Index;
752
Reid Klecknerb5180542017-03-21 16:57:19 +0000753 if (Attrs.hasAttrSomewhere(Attribute::Returned, &Index) && Index)
Reid Klecknera0b45f42017-05-03 18:17:31 +0000754 return getArgOperand(Index - AttributeList::FirstArgIndex);
Hal Finkele87ad542016-07-10 23:01:32 +0000755 if (const Function *F = getCalledFunction())
756 if (F->getAttributes().hasAttrSomewhere(Attribute::Returned, &Index) &&
757 Index)
Reid Klecknera0b45f42017-05-03 18:17:31 +0000758 return getArgOperand(Index - AttributeList::FirstArgIndex);
759
Hal Finkele87ad542016-07-10 23:01:32 +0000760 return nullptr;
761}
762
Reid Klecknerfb502d22017-04-14 20:19:02 +0000763bool InvokeInst::hasRetAttr(Attribute::AttrKind Kind) const {
764 if (Attrs.hasAttribute(AttributeList::ReturnIndex, Kind))
765 return true;
Sanjoy Dasb11b4402015-11-04 20:33:45 +0000766
Reid Klecknerfb502d22017-04-14 20:19:02 +0000767 // Look at the callee, if available.
768 if (const Function *F = getCalledFunction())
769 return F->getAttributes().hasAttribute(AttributeList::ReturnIndex, Kind);
770 return false;
771}
772
773bool InvokeInst::paramHasAttr(unsigned i, Attribute::AttrKind Kind) const {
774 assert(i < getNumArgOperands() && "Param index out of bounds!");
775
776 if (Attrs.hasParamAttribute(i, Kind))
Bill Wendling8baa61d2012-10-03 17:54:26 +0000777 return true;
778 if (const Function *F = getCalledFunction())
Reid Klecknerfb502d22017-04-14 20:19:02 +0000779 return F->getAttributes().hasParamAttribute(i, Kind);
Bill Wendlingdaf8e382012-10-04 07:18:12 +0000780 return false;
781}
782
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000783bool InvokeInst::dataOperandHasImpliedAttr(unsigned i,
Amaury Sechet392638d2016-06-14 20:27:35 +0000784 Attribute::AttrKind Kind) const {
Sanjoy Das776e4a72015-11-05 01:53:26 +0000785 // There are getNumOperands() - 3 data operands. The last three operands are
786 // the callee and the two successor basic blocks.
787 assert(i < (getNumOperands() - 2) && "Data operand index out of bounds!");
788
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000789 // The attribute A can either be directly specified, if the operand in
790 // question is an invoke argument; or be indirectly implied by the kind of its
791 // containing operand bundle, if the operand is a bundle operand.
792
Reid Kleckner545aa4f2017-05-23 17:03:28 +0000793 if (i == AttributeList::ReturnIndex)
Reid Kleckner8bf67fe2017-05-23 17:01:48 +0000794 return hasRetAttr(Kind);
795
Reid Klecknerfb502d22017-04-14 20:19:02 +0000796 // FIXME: Avoid these i - 1 calculations and update the API to use zero-based
797 // indices.
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000798 if (i < (getNumArgOperands() + 1))
Reid Klecknerfb502d22017-04-14 20:19:02 +0000799 return paramHasAttr(i - 1, Kind);
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000800
801 assert(hasOperandBundles() && i >= (getBundleOperandsStartIndex() + 1) &&
802 "Must be either an invoke argument or an operand bundle!");
Amaury Sechet392638d2016-06-14 20:27:35 +0000803 return bundleOperandHasAttr(i - 1, Kind);
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000804}
805
Amaury Sechet392638d2016-06-14 20:27:35 +0000806void InvokeInst::addAttribute(unsigned i, Attribute::AttrKind Kind) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000807 AttributeList PAL = getAttributes();
Amaury Sechet392638d2016-06-14 20:27:35 +0000808 PAL = PAL.addAttribute(getContext(), i, Kind);
Devang Patel4c758ea2008-09-25 21:00:45 +0000809 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000810}
811
Amaury Secheta65a2372016-06-15 05:14:29 +0000812void InvokeInst::addAttribute(unsigned i, Attribute Attr) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000813 AttributeList PAL = getAttributes();
Amaury Secheta65a2372016-06-15 05:14:29 +0000814 PAL = PAL.addAttribute(getContext(), i, Attr);
815 setAttributes(PAL);
816}
817
Reid Klecknera0b45f42017-05-03 18:17:31 +0000818void InvokeInst::addParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) {
Reid Kleckner5fbdd172017-05-31 19:23:09 +0000819 AttributeList PAL = getAttributes();
820 PAL = PAL.addParamAttribute(getContext(), ArgNo, Kind);
821 setAttributes(PAL);
Reid Klecknera0b45f42017-05-03 18:17:31 +0000822}
823
Amaury Sechet392638d2016-06-14 20:27:35 +0000824void InvokeInst::removeAttribute(unsigned i, Attribute::AttrKind Kind) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000825 AttributeList PAL = getAttributes();
Amaury Sechet392638d2016-06-14 20:27:35 +0000826 PAL = PAL.removeAttribute(getContext(), i, Kind);
Amaury Sechet1a0e0972016-04-21 21:29:10 +0000827 setAttributes(PAL);
828}
829
Amaury Sechet6100adf2016-06-15 17:50:39 +0000830void InvokeInst::removeAttribute(unsigned i, StringRef Kind) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000831 AttributeList PAL = getAttributes();
Amaury Sechet6100adf2016-06-15 17:50:39 +0000832 PAL = PAL.removeAttribute(getContext(), i, Kind);
833 setAttributes(PAL);
834}
835
Reid Klecknera0b45f42017-05-03 18:17:31 +0000836void InvokeInst::removeParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) {
Reid Kleckner5fbdd172017-05-31 19:23:09 +0000837 AttributeList PAL = getAttributes();
838 PAL = PAL.removeParamAttribute(getContext(), ArgNo, Kind);
839 setAttributes(PAL);
Reid Klecknera0b45f42017-05-03 18:17:31 +0000840}
841
Ramkumar Ramachandra8fcb4982015-02-14 19:37:54 +0000842void InvokeInst::addDereferenceableAttr(unsigned i, uint64_t Bytes) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000843 AttributeList PAL = getAttributes();
Ramkumar Ramachandra8fcb4982015-02-14 19:37:54 +0000844 PAL = PAL.addDereferenceableAttr(getContext(), i, Bytes);
845 setAttributes(PAL);
846}
847
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000848void InvokeInst::addDereferenceableOrNullAttr(unsigned i, uint64_t Bytes) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000849 AttributeList PAL = getAttributes();
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000850 PAL = PAL.addDereferenceableOrNullAttr(getContext(), i, Bytes);
851 setAttributes(PAL);
852}
853
Bill Wendlingfae14752011-08-12 20:24:12 +0000854LandingPadInst *InvokeInst::getLandingPadInst() const {
855 return cast<LandingPadInst>(getUnwindDest()->getFirstNonPHI());
856}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000857
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000858//===----------------------------------------------------------------------===//
859// ReturnInst Implementation
860//===----------------------------------------------------------------------===//
861
Chris Lattner2195fc42007-02-24 00:55:48 +0000862ReturnInst::ReturnInst(const ReturnInst &RI)
Owen Anderson55f1c092009-08-13 21:58:54 +0000863 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000864 OperandTraits<ReturnInst>::op_end(this) -
865 RI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000866 RI.getNumOperands()) {
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000867 if (RI.getNumOperands())
Gabor Greif2d3024d2008-05-26 21:33:52 +0000868 Op<0>() = RI.Op<0>();
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000869 SubclassOptionalData = RI.SubclassOptionalData;
Chris Lattner2195fc42007-02-24 00:55:48 +0000870}
871
Owen Anderson55f1c092009-08-13 21:58:54 +0000872ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, Instruction *InsertBefore)
873 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000874 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
875 InsertBefore) {
Devang Patelc38eb522008-02-26 18:49:29 +0000876 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000877 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000878}
Eugene Zelenkod761e2c2017-05-15 21:57:41 +0000879
Owen Anderson55f1c092009-08-13 21:58:54 +0000880ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd)
881 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000882 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
883 InsertAtEnd) {
Devang Patelc38eb522008-02-26 18:49:29 +0000884 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000885 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000886}
Eugene Zelenkod761e2c2017-05-15 21:57:41 +0000887
Owen Anderson55f1c092009-08-13 21:58:54 +0000888ReturnInst::ReturnInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
889 : TerminatorInst(Type::getVoidTy(Context), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000890 OperandTraits<ReturnInst>::op_end(this), 0, InsertAtEnd) {
Devang Patel59643e52008-02-23 00:35:18 +0000891}
892
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000893//===----------------------------------------------------------------------===//
Bill Wendlingf891bf82011-07-31 06:30:59 +0000894// ResumeInst Implementation
895//===----------------------------------------------------------------------===//
896
897ResumeInst::ResumeInst(const ResumeInst &RI)
898 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Resume,
899 OperandTraits<ResumeInst>::op_begin(this), 1) {
900 Op<0>() = RI.Op<0>();
901}
902
903ResumeInst::ResumeInst(Value *Exn, Instruction *InsertBefore)
904 : TerminatorInst(Type::getVoidTy(Exn->getContext()), Instruction::Resume,
905 OperandTraits<ResumeInst>::op_begin(this), 1, InsertBefore) {
906 Op<0>() = Exn;
907}
908
909ResumeInst::ResumeInst(Value *Exn, BasicBlock *InsertAtEnd)
910 : TerminatorInst(Type::getVoidTy(Exn->getContext()), Instruction::Resume,
911 OperandTraits<ResumeInst>::op_begin(this), 1, InsertAtEnd) {
912 Op<0>() = Exn;
913}
914
Bill Wendlingf891bf82011-07-31 06:30:59 +0000915//===----------------------------------------------------------------------===//
David Majnemer654e1302015-07-31 17:58:14 +0000916// CleanupReturnInst Implementation
917//===----------------------------------------------------------------------===//
918
919CleanupReturnInst::CleanupReturnInst(const CleanupReturnInst &CRI)
920 : TerminatorInst(CRI.getType(), Instruction::CleanupRet,
921 OperandTraits<CleanupReturnInst>::op_end(this) -
922 CRI.getNumOperands(),
923 CRI.getNumOperands()) {
David Majnemereb518bd2015-08-04 08:21:40 +0000924 setInstructionSubclassData(CRI.getSubclassDataFromInstruction());
David Majnemer8a1c45d2015-12-12 05:38:55 +0000925 Op<0>() = CRI.Op<0>();
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000926 if (CRI.hasUnwindDest())
David Majnemer8a1c45d2015-12-12 05:38:55 +0000927 Op<1>() = CRI.Op<1>();
David Majnemer654e1302015-07-31 17:58:14 +0000928}
929
David Majnemer8a1c45d2015-12-12 05:38:55 +0000930void CleanupReturnInst::init(Value *CleanupPad, BasicBlock *UnwindBB) {
David Majnemer654e1302015-07-31 17:58:14 +0000931 if (UnwindBB)
932 setInstructionSubclassData(getSubclassDataFromInstruction() | 1);
David Majnemer654e1302015-07-31 17:58:14 +0000933
David Majnemer8a1c45d2015-12-12 05:38:55 +0000934 Op<0>() = CleanupPad;
David Majnemer654e1302015-07-31 17:58:14 +0000935 if (UnwindBB)
David Majnemer8a1c45d2015-12-12 05:38:55 +0000936 Op<1>() = UnwindBB;
David Majnemer654e1302015-07-31 17:58:14 +0000937}
938
David Majnemer8a1c45d2015-12-12 05:38:55 +0000939CleanupReturnInst::CleanupReturnInst(Value *CleanupPad, BasicBlock *UnwindBB,
940 unsigned Values, Instruction *InsertBefore)
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000941 : TerminatorInst(Type::getVoidTy(CleanupPad->getContext()),
942 Instruction::CleanupRet,
David Majnemer654e1302015-07-31 17:58:14 +0000943 OperandTraits<CleanupReturnInst>::op_end(this) - Values,
944 Values, InsertBefore) {
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000945 init(CleanupPad, UnwindBB);
David Majnemer654e1302015-07-31 17:58:14 +0000946}
947
David Majnemer8a1c45d2015-12-12 05:38:55 +0000948CleanupReturnInst::CleanupReturnInst(Value *CleanupPad, BasicBlock *UnwindBB,
949 unsigned Values, BasicBlock *InsertAtEnd)
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000950 : TerminatorInst(Type::getVoidTy(CleanupPad->getContext()),
951 Instruction::CleanupRet,
David Majnemer654e1302015-07-31 17:58:14 +0000952 OperandTraits<CleanupReturnInst>::op_end(this) - Values,
953 Values, InsertAtEnd) {
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000954 init(CleanupPad, UnwindBB);
David Majnemer654e1302015-07-31 17:58:14 +0000955}
956
David Majnemer654e1302015-07-31 17:58:14 +0000957//===----------------------------------------------------------------------===//
David Majnemer654e1302015-07-31 17:58:14 +0000958// CatchReturnInst Implementation
959//===----------------------------------------------------------------------===//
David Majnemer8a1c45d2015-12-12 05:38:55 +0000960void CatchReturnInst::init(Value *CatchPad, BasicBlock *BB) {
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000961 Op<0>() = CatchPad;
962 Op<1>() = BB;
David Majnemer0bc0eef2015-08-15 02:46:08 +0000963}
David Majnemer654e1302015-07-31 17:58:14 +0000964
965CatchReturnInst::CatchReturnInst(const CatchReturnInst &CRI)
966 : TerminatorInst(Type::getVoidTy(CRI.getContext()), Instruction::CatchRet,
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000967 OperandTraits<CatchReturnInst>::op_begin(this), 2) {
968 Op<0>() = CRI.Op<0>();
969 Op<1>() = CRI.Op<1>();
David Majnemer654e1302015-07-31 17:58:14 +0000970}
971
David Majnemer8a1c45d2015-12-12 05:38:55 +0000972CatchReturnInst::CatchReturnInst(Value *CatchPad, BasicBlock *BB,
David Majnemer0bc0eef2015-08-15 02:46:08 +0000973 Instruction *InsertBefore)
David Majnemer654e1302015-07-31 17:58:14 +0000974 : TerminatorInst(Type::getVoidTy(BB->getContext()), Instruction::CatchRet,
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000975 OperandTraits<CatchReturnInst>::op_begin(this), 2,
976 InsertBefore) {
977 init(CatchPad, BB);
David Majnemer654e1302015-07-31 17:58:14 +0000978}
979
David Majnemer8a1c45d2015-12-12 05:38:55 +0000980CatchReturnInst::CatchReturnInst(Value *CatchPad, BasicBlock *BB,
David Majnemer0bc0eef2015-08-15 02:46:08 +0000981 BasicBlock *InsertAtEnd)
David Majnemer654e1302015-07-31 17:58:14 +0000982 : TerminatorInst(Type::getVoidTy(BB->getContext()), Instruction::CatchRet,
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000983 OperandTraits<CatchReturnInst>::op_begin(this), 2,
984 InsertAtEnd) {
985 init(CatchPad, BB);
David Majnemer654e1302015-07-31 17:58:14 +0000986}
987
David Majnemer654e1302015-07-31 17:58:14 +0000988//===----------------------------------------------------------------------===//
David Majnemer8a1c45d2015-12-12 05:38:55 +0000989// CatchSwitchInst Implementation
David Majnemer654e1302015-07-31 17:58:14 +0000990//===----------------------------------------------------------------------===//
David Majnemer8a1c45d2015-12-12 05:38:55 +0000991
992CatchSwitchInst::CatchSwitchInst(Value *ParentPad, BasicBlock *UnwindDest,
993 unsigned NumReservedValues,
994 const Twine &NameStr,
995 Instruction *InsertBefore)
996 : TerminatorInst(ParentPad->getType(), Instruction::CatchSwitch, nullptr, 0,
997 InsertBefore) {
998 if (UnwindDest)
999 ++NumReservedValues;
1000 init(ParentPad, UnwindDest, NumReservedValues + 1);
David Majnemer654e1302015-07-31 17:58:14 +00001001 setName(NameStr);
1002}
1003
David Majnemer8a1c45d2015-12-12 05:38:55 +00001004CatchSwitchInst::CatchSwitchInst(Value *ParentPad, BasicBlock *UnwindDest,
1005 unsigned NumReservedValues,
1006 const Twine &NameStr, BasicBlock *InsertAtEnd)
1007 : TerminatorInst(ParentPad->getType(), Instruction::CatchSwitch, nullptr, 0,
David Majnemer5c73c942015-08-13 22:11:40 +00001008 InsertAtEnd) {
David Majnemer8a1c45d2015-12-12 05:38:55 +00001009 if (UnwindDest)
1010 ++NumReservedValues;
1011 init(ParentPad, UnwindDest, NumReservedValues + 1);
1012 setName(NameStr);
David Majnemer654e1302015-07-31 17:58:14 +00001013}
1014
David Majnemer8a1c45d2015-12-12 05:38:55 +00001015CatchSwitchInst::CatchSwitchInst(const CatchSwitchInst &CSI)
1016 : TerminatorInst(CSI.getType(), Instruction::CatchSwitch, nullptr,
1017 CSI.getNumOperands()) {
1018 init(CSI.getParentPad(), CSI.getUnwindDest(), CSI.getNumOperands());
1019 setNumHungOffUseOperands(ReservedSpace);
1020 Use *OL = getOperandList();
1021 const Use *InOL = CSI.getOperandList();
1022 for (unsigned I = 1, E = ReservedSpace; I != E; ++I)
1023 OL[I] = InOL[I];
David Majnemer654e1302015-07-31 17:58:14 +00001024}
David Majnemer8a1c45d2015-12-12 05:38:55 +00001025
1026void CatchSwitchInst::init(Value *ParentPad, BasicBlock *UnwindDest,
1027 unsigned NumReservedValues) {
1028 assert(ParentPad && NumReservedValues);
1029
1030 ReservedSpace = NumReservedValues;
1031 setNumHungOffUseOperands(UnwindDest ? 2 : 1);
1032 allocHungoffUses(ReservedSpace);
1033
1034 Op<0>() = ParentPad;
1035 if (UnwindDest) {
1036 setInstructionSubclassData(getSubclassDataFromInstruction() | 1);
1037 setUnwindDest(UnwindDest);
1038 }
1039}
1040
1041/// growOperands - grow operands - This grows the operand list in response to a
1042/// push_back style of operation. This grows the number of ops by 2 times.
1043void CatchSwitchInst::growOperands(unsigned Size) {
1044 unsigned NumOperands = getNumOperands();
1045 assert(NumOperands >= 1);
1046 if (ReservedSpace >= NumOperands + Size)
1047 return;
1048 ReservedSpace = (NumOperands + Size / 2) * 2;
1049 growHungoffUses(ReservedSpace);
1050}
1051
1052void CatchSwitchInst::addHandler(BasicBlock *Handler) {
1053 unsigned OpNo = getNumOperands();
1054 growOperands(1);
1055 assert(OpNo < ReservedSpace && "Growing didn't work!");
1056 setNumHungOffUseOperands(getNumOperands() + 1);
1057 getOperandList()[OpNo] = Handler;
1058}
1059
Joseph Tremoulet0d808882016-01-05 02:37:41 +00001060void CatchSwitchInst::removeHandler(handler_iterator HI) {
1061 // Move all subsequent handlers up one.
1062 Use *EndDst = op_end() - 1;
1063 for (Use *CurDst = HI.getCurrent(); CurDst != EndDst; ++CurDst)
1064 *CurDst = *(CurDst + 1);
1065 // Null out the last handler use.
1066 *EndDst = nullptr;
1067
1068 setNumHungOffUseOperands(getNumOperands() - 1);
1069}
1070
David Majnemer8a1c45d2015-12-12 05:38:55 +00001071//===----------------------------------------------------------------------===//
1072// FuncletPadInst Implementation
1073//===----------------------------------------------------------------------===//
1074void FuncletPadInst::init(Value *ParentPad, ArrayRef<Value *> Args,
1075 const Twine &NameStr) {
1076 assert(getNumOperands() == 1 + Args.size() && "NumOperands not set up?");
1077 std::copy(Args.begin(), Args.end(), op_begin());
1078 setParentPad(ParentPad);
1079 setName(NameStr);
1080}
1081
1082FuncletPadInst::FuncletPadInst(const FuncletPadInst &FPI)
1083 : Instruction(FPI.getType(), FPI.getOpcode(),
1084 OperandTraits<FuncletPadInst>::op_end(this) -
1085 FPI.getNumOperands(),
1086 FPI.getNumOperands()) {
1087 std::copy(FPI.op_begin(), FPI.op_end(), op_begin());
1088 setParentPad(FPI.getParentPad());
1089}
1090
1091FuncletPadInst::FuncletPadInst(Instruction::FuncletPadOps Op, Value *ParentPad,
1092 ArrayRef<Value *> Args, unsigned Values,
1093 const Twine &NameStr, Instruction *InsertBefore)
1094 : Instruction(ParentPad->getType(), Op,
1095 OperandTraits<FuncletPadInst>::op_end(this) - Values, Values,
1096 InsertBefore) {
1097 init(ParentPad, Args, NameStr);
1098}
1099
1100FuncletPadInst::FuncletPadInst(Instruction::FuncletPadOps Op, Value *ParentPad,
1101 ArrayRef<Value *> Args, unsigned Values,
1102 const Twine &NameStr, BasicBlock *InsertAtEnd)
1103 : Instruction(ParentPad->getType(), Op,
1104 OperandTraits<FuncletPadInst>::op_end(this) - Values, Values,
1105 InsertAtEnd) {
1106 init(ParentPad, Args, NameStr);
David Majnemer654e1302015-07-31 17:58:14 +00001107}
1108
1109//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +00001110// UnreachableInst Implementation
1111//===----------------------------------------------------------------------===//
1112
Owen Anderson55f1c092009-08-13 21:58:54 +00001113UnreachableInst::UnreachableInst(LLVMContext &Context,
1114 Instruction *InsertBefore)
1115 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
Craig Topperc6207612014-04-09 06:08:46 +00001116 nullptr, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +00001117}
Owen Anderson55f1c092009-08-13 21:58:54 +00001118UnreachableInst::UnreachableInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
1119 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
Craig Topperc6207612014-04-09 06:08:46 +00001120 nullptr, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +00001121}
1122
Chris Lattner5e0b9f22004-10-16 18:08:06 +00001123//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001124// BranchInst Implementation
1125//===----------------------------------------------------------------------===//
1126
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001127void BranchInst::AssertOK() {
1128 if (isConditional())
Duncan Sands9dff9be2010-02-15 16:12:20 +00001129 assert(getCondition()->getType()->isIntegerTy(1) &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001130 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001131}
1132
Chris Lattner2195fc42007-02-24 00:55:48 +00001133BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001134 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +00001135 OperandTraits<BranchInst>::op_end(this) - 1,
1136 1, InsertBefore) {
Craig Topper2617dcc2014-04-15 06:32:26 +00001137 assert(IfTrue && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001138 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +00001139}
Eugene Zelenkod761e2c2017-05-15 21:57:41 +00001140
Chris Lattner2195fc42007-02-24 00:55:48 +00001141BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
1142 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001143 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +00001144 OperandTraits<BranchInst>::op_end(this) - 3,
1145 3, InsertBefore) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001146 Op<-1>() = IfTrue;
1147 Op<-2>() = IfFalse;
1148 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +00001149#ifndef NDEBUG
1150 AssertOK();
1151#endif
1152}
1153
1154BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001155 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +00001156 OperandTraits<BranchInst>::op_end(this) - 1,
1157 1, InsertAtEnd) {
Craig Topper2617dcc2014-04-15 06:32:26 +00001158 assert(IfTrue && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001159 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +00001160}
1161
1162BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
1163 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001164 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +00001165 OperandTraits<BranchInst>::op_end(this) - 3,
1166 3, InsertAtEnd) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001167 Op<-1>() = IfTrue;
1168 Op<-2>() = IfFalse;
1169 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +00001170#ifndef NDEBUG
1171 AssertOK();
1172#endif
1173}
1174
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001175BranchInst::BranchInst(const BranchInst &BI) :
Owen Anderson55f1c092009-08-13 21:58:54 +00001176 TerminatorInst(Type::getVoidTy(BI.getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +00001177 OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(),
1178 BI.getNumOperands()) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001179 Op<-1>() = BI.Op<-1>();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001180 if (BI.getNumOperands() != 1) {
1181 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001182 Op<-3>() = BI.Op<-3>();
1183 Op<-2>() = BI.Op<-2>();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001184 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001185 SubclassOptionalData = BI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001186}
1187
Chandler Carruth3e8aa652011-10-17 01:11:57 +00001188void BranchInst::swapSuccessors() {
1189 assert(isConditional() &&
1190 "Cannot swap successors of an unconditional branch");
1191 Op<-1>().swap(Op<-2>());
1192
1193 // Update profile metadata if present and it matches our structural
1194 // expectations.
Xinliang David Lidc491402016-08-23 15:39:03 +00001195 swapProfMetadata();
Chandler Carruth3e8aa652011-10-17 01:11:57 +00001196}
1197
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001198//===----------------------------------------------------------------------===//
Victor Hernandez8acf2952009-10-23 21:09:37 +00001199// AllocaInst Implementation
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001200//===----------------------------------------------------------------------===//
1201
Owen Andersonb6b25302009-07-14 23:09:55 +00001202static Value *getAISize(LLVMContext &Context, Value *Amt) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001203 if (!Amt)
Owen Anderson55f1c092009-08-13 21:58:54 +00001204 Amt = ConstantInt::get(Type::getInt32Ty(Context), 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +00001205 else {
1206 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +00001207 "Passed basic block into allocation size parameter! Use other ctor");
Dan Gohman2140a742010-05-28 01:14:11 +00001208 assert(Amt->getType()->isIntegerTy() &&
1209 "Allocation array size is not an integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +00001210 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001211 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001212}
1213
Matt Arsenault3c1fc762017-04-10 22:27:50 +00001214AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, const Twine &Name,
Victor Hernandez8acf2952009-10-23 21:09:37 +00001215 Instruction *InsertBefore)
Matt Arsenault3c1fc762017-04-10 22:27:50 +00001216 : AllocaInst(Ty, AddrSpace, /*ArraySize=*/nullptr, Name, InsertBefore) {}
Victor Hernandez8acf2952009-10-23 21:09:37 +00001217
Matt Arsenault3c1fc762017-04-10 22:27:50 +00001218AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, const Twine &Name,
Victor Hernandez8acf2952009-10-23 21:09:37 +00001219 BasicBlock *InsertAtEnd)
Matt Arsenault3c1fc762017-04-10 22:27:50 +00001220 : AllocaInst(Ty, AddrSpace, /*ArraySize=*/nullptr, Name, InsertAtEnd) {}
Victor Hernandez8acf2952009-10-23 21:09:37 +00001221
Matt Arsenault3c1fc762017-04-10 22:27:50 +00001222AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize,
Victor Hernandez8acf2952009-10-23 21:09:37 +00001223 const Twine &Name, Instruction *InsertBefore)
Matt Arsenault3c1fc762017-04-10 22:27:50 +00001224 : AllocaInst(Ty, AddrSpace, ArraySize, /*Align=*/0, Name, InsertBefore) {}
1225
1226AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize,
1227 const Twine &Name, BasicBlock *InsertAtEnd)
1228 : AllocaInst(Ty, AddrSpace, ArraySize, /*Align=*/0, Name, InsertAtEnd) {}
1229
1230AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize,
1231 unsigned Align, const Twine &Name,
1232 Instruction *InsertBefore)
1233 : UnaryInstruction(PointerType::get(Ty, AddrSpace), Alloca,
1234 getAISize(Ty->getContext(), ArraySize), InsertBefore),
1235 AllocatedType(Ty) {
Dan Gohmanaa583d72008-03-24 16:55:58 +00001236 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +00001237 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +00001238 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001239}
1240
Matt Arsenault3c1fc762017-04-10 22:27:50 +00001241AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize,
1242 unsigned Align, const Twine &Name,
1243 BasicBlock *InsertAtEnd)
1244 : UnaryInstruction(PointerType::get(Ty, AddrSpace), Alloca,
1245 getAISize(Ty->getContext(), ArraySize), InsertAtEnd),
David Blaikiebf0a42a2015-04-29 23:00:35 +00001246 AllocatedType(Ty) {
Dan Gohmanaa583d72008-03-24 16:55:58 +00001247 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +00001248 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +00001249 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001250}
1251
Victor Hernandez8acf2952009-10-23 21:09:37 +00001252void AllocaInst::setAlignment(unsigned Align) {
Dan Gohmanaa583d72008-03-24 16:55:58 +00001253 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001254 assert(Align <= MaximumAlignment &&
1255 "Alignment is greater than MaximumAlignment!");
Reid Kleckner436c42e2014-01-17 23:58:17 +00001256 setInstructionSubclassData((getSubclassDataFromInstruction() & ~31) |
1257 (Log2_32(Align) + 1));
Dan Gohmanaa583d72008-03-24 16:55:58 +00001258 assert(getAlignment() == Align && "Alignment representation error!");
1259}
1260
Victor Hernandez8acf2952009-10-23 21:09:37 +00001261bool AllocaInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +00001262 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
Dan Gohman9a1b8592010-09-27 15:15:44 +00001263 return !CI->isOne();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001264 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001265}
1266
Chris Lattner8b291e62008-11-26 02:54:17 +00001267/// isStaticAlloca - Return true if this alloca is in the entry block of the
1268/// function and is a constant size. If so, the code generator will fold it
1269/// into the prolog/epilog code, so it is basically free.
1270bool AllocaInst::isStaticAlloca() const {
1271 // Must be constant size.
1272 if (!isa<ConstantInt>(getArraySize())) return false;
1273
1274 // Must be in the entry block.
1275 const BasicBlock *Parent = getParent();
Reid Kleckner436c42e2014-01-17 23:58:17 +00001276 return Parent == &Parent->getParent()->front() && !isUsedWithInAlloca();
Chris Lattner8b291e62008-11-26 02:54:17 +00001277}
1278
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001279//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001280// LoadInst Implementation
1281//===----------------------------------------------------------------------===//
1282
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001283void LoadInst::AssertOK() {
Duncan Sands19d0b472010-02-16 11:11:14 +00001284 assert(getOperand(0)->getType()->isPointerTy() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001285 "Ptr must have pointer type.");
Eli Friedman59b66882011-08-09 23:02:53 +00001286 assert(!(isAtomic() && getAlignment() == 0) &&
1287 "Alignment required for atomic load");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001288}
1289
Daniel Dunbar4975db62009-07-25 04:41:11 +00001290LoadInst::LoadInst(Value *Ptr, const Twine &Name, Instruction *InsertBef)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001291 : LoadInst(Ptr, Name, /*isVolatile=*/false, InsertBef) {}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001292
Daniel Dunbar4975db62009-07-25 04:41:11 +00001293LoadInst::LoadInst(Value *Ptr, const Twine &Name, BasicBlock *InsertAE)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001294 : LoadInst(Ptr, Name, /*isVolatile=*/false, InsertAE) {}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001295
David Blaikie0c28fd72015-05-20 21:46:30 +00001296LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001297 Instruction *InsertBef)
David Blaikie0c28fd72015-05-20 21:46:30 +00001298 : LoadInst(Ty, Ptr, Name, isVolatile, /*Align=*/0, InsertBef) {}
Eli Friedman59b66882011-08-09 23:02:53 +00001299
1300LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
1301 BasicBlock *InsertAE)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001302 : LoadInst(Ptr, Name, isVolatile, /*Align=*/0, InsertAE) {}
Christopher Lamb84485702007-04-22 19:24:39 +00001303
David Blaikieb7a029872015-04-17 19:56:21 +00001304LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Christopher Lamb84485702007-04-22 19:24:39 +00001305 unsigned Align, Instruction *InsertBef)
JF Bastien800f87a2016-04-06 21:19:33 +00001306 : LoadInst(Ty, Ptr, Name, isVolatile, Align, AtomicOrdering::NotAtomic,
1307 CrossThread, InsertBef) {}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001308
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001309LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +00001310 unsigned Align, BasicBlock *InsertAE)
JF Bastien800f87a2016-04-06 21:19:33 +00001311 : LoadInst(Ptr, Name, isVolatile, Align, AtomicOrdering::NotAtomic,
1312 CrossThread, InsertAE) {}
Dan Gohman68659282007-07-18 20:51:11 +00001313
David Blaikie15d9a4c2015-04-06 20:59:48 +00001314LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001315 unsigned Align, AtomicOrdering Order,
David Blaikie15d9a4c2015-04-06 20:59:48 +00001316 SynchronizationScope SynchScope, Instruction *InsertBef)
1317 : UnaryInstruction(Ty, Load, Ptr, InsertBef) {
David Blaikie79009f82015-05-20 20:22:31 +00001318 assert(Ty == cast<PointerType>(Ptr->getType())->getElementType());
Eli Friedman59b66882011-08-09 23:02:53 +00001319 setVolatile(isVolatile);
1320 setAlignment(Align);
1321 setAtomic(Order, SynchScope);
1322 AssertOK();
1323 setName(Name);
1324}
1325
1326LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
1327 unsigned Align, AtomicOrdering Order,
1328 SynchronizationScope SynchScope,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001329 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001330 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001331 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +00001332 setVolatile(isVolatile);
Eli Friedman59b66882011-08-09 23:02:53 +00001333 setAlignment(Align);
1334 setAtomic(Order, SynchScope);
Chris Lattner0f048162007-02-13 07:54:42 +00001335 AssertOK();
1336 setName(Name);
1337}
1338
Daniel Dunbar27096822009-08-11 18:11:15 +00001339LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
1340 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1341 Load, Ptr, InsertBef) {
1342 setVolatile(false);
1343 setAlignment(0);
JF Bastien800f87a2016-04-06 21:19:33 +00001344 setAtomic(AtomicOrdering::NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001345 AssertOK();
1346 if (Name && Name[0]) setName(Name);
1347}
1348
1349LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
1350 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1351 Load, Ptr, InsertAE) {
1352 setVolatile(false);
1353 setAlignment(0);
JF Bastien800f87a2016-04-06 21:19:33 +00001354 setAtomic(AtomicOrdering::NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001355 AssertOK();
1356 if (Name && Name[0]) setName(Name);
1357}
1358
David Blaikie0c28fd72015-05-20 21:46:30 +00001359LoadInst::LoadInst(Type *Ty, Value *Ptr, const char *Name, bool isVolatile,
Daniel Dunbar27096822009-08-11 18:11:15 +00001360 Instruction *InsertBef)
David Blaikie0c28fd72015-05-20 21:46:30 +00001361 : UnaryInstruction(Ty, Load, Ptr, InsertBef) {
1362 assert(Ty == cast<PointerType>(Ptr->getType())->getElementType());
Daniel Dunbar27096822009-08-11 18:11:15 +00001363 setVolatile(isVolatile);
1364 setAlignment(0);
JF Bastien800f87a2016-04-06 21:19:33 +00001365 setAtomic(AtomicOrdering::NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001366 AssertOK();
1367 if (Name && Name[0]) setName(Name);
1368}
1369
1370LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
1371 BasicBlock *InsertAE)
1372 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1373 Load, Ptr, InsertAE) {
1374 setVolatile(isVolatile);
1375 setAlignment(0);
JF Bastien800f87a2016-04-06 21:19:33 +00001376 setAtomic(AtomicOrdering::NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001377 AssertOK();
1378 if (Name && Name[0]) setName(Name);
1379}
1380
Christopher Lamb84485702007-04-22 19:24:39 +00001381void LoadInst::setAlignment(unsigned Align) {
1382 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001383 assert(Align <= MaximumAlignment &&
1384 "Alignment is greater than MaximumAlignment!");
Eli Friedman59b66882011-08-09 23:02:53 +00001385 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) |
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001386 ((Log2_32(Align)+1)<<1));
Dan Gohmana7e5a242010-07-28 20:12:04 +00001387 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +00001388}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001389
1390//===----------------------------------------------------------------------===//
1391// StoreInst Implementation
1392//===----------------------------------------------------------------------===//
1393
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001394void StoreInst::AssertOK() {
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001395 assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001396 assert(getOperand(1)->getType()->isPointerTy() &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001397 "Ptr must have pointer type!");
1398 assert(getOperand(0)->getType() ==
1399 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +00001400 && "Ptr must be a pointer to Val type!");
Eli Friedman59b66882011-08-09 23:02:53 +00001401 assert(!(isAtomic() && getAlignment() == 0) &&
Mark Lacey1d7c97e2013-12-21 00:00:49 +00001402 "Alignment required for atomic store");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001403}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001404
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001405StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001406 : StoreInst(val, addr, /*isVolatile=*/false, InsertBefore) {}
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001407
1408StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001409 : StoreInst(val, addr, /*isVolatile=*/false, InsertAtEnd) {}
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001410
Misha Brukmanb1c93172005-04-21 23:48:37 +00001411StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001412 Instruction *InsertBefore)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001413 : StoreInst(val, addr, isVolatile, /*Align=*/0, InsertBefore) {}
Christopher Lamb84485702007-04-22 19:24:39 +00001414
1415StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001416 BasicBlock *InsertAtEnd)
1417 : StoreInst(val, addr, isVolatile, /*Align=*/0, InsertAtEnd) {}
1418
1419StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, unsigned Align,
1420 Instruction *InsertBefore)
JF Bastien800f87a2016-04-06 21:19:33 +00001421 : StoreInst(val, addr, isVolatile, Align, AtomicOrdering::NotAtomic,
1422 CrossThread, InsertBefore) {}
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001423
1424StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, unsigned Align,
1425 BasicBlock *InsertAtEnd)
JF Bastien800f87a2016-04-06 21:19:33 +00001426 : StoreInst(val, addr, isVolatile, Align, AtomicOrdering::NotAtomic,
1427 CrossThread, InsertAtEnd) {}
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001428
Misha Brukmanb1c93172005-04-21 23:48:37 +00001429StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001430 unsigned Align, AtomicOrdering Order,
1431 SynchronizationScope SynchScope,
1432 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001433 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001434 OperandTraits<StoreInst>::op_begin(this),
1435 OperandTraits<StoreInst>::operands(this),
Eli Friedman59b66882011-08-09 23:02:53 +00001436 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001437 Op<0>() = val;
1438 Op<1>() = addr;
Dan Gohman68659282007-07-18 20:51:11 +00001439 setVolatile(isVolatile);
1440 setAlignment(Align);
Eli Friedman59b66882011-08-09 23:02:53 +00001441 setAtomic(Order, SynchScope);
Dan Gohman68659282007-07-18 20:51:11 +00001442 AssertOK();
1443}
1444
1445StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001446 unsigned Align, AtomicOrdering Order,
1447 SynchronizationScope SynchScope,
1448 BasicBlock *InsertAtEnd)
1449 : Instruction(Type::getVoidTy(val->getContext()), Store,
1450 OperandTraits<StoreInst>::op_begin(this),
1451 OperandTraits<StoreInst>::operands(this),
1452 InsertAtEnd) {
1453 Op<0>() = val;
1454 Op<1>() = addr;
1455 setVolatile(isVolatile);
1456 setAlignment(Align);
1457 setAtomic(Order, SynchScope);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001458 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001459}
1460
Christopher Lamb84485702007-04-22 19:24:39 +00001461void StoreInst::setAlignment(unsigned Align) {
1462 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001463 assert(Align <= MaximumAlignment &&
1464 "Alignment is greater than MaximumAlignment!");
Eli Friedman59b66882011-08-09 23:02:53 +00001465 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) |
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001466 ((Log2_32(Align)+1) << 1));
Dan Gohmana7e5a242010-07-28 20:12:04 +00001467 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +00001468}
1469
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001470//===----------------------------------------------------------------------===//
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001471// AtomicCmpXchgInst Implementation
1472//===----------------------------------------------------------------------===//
1473
1474void AtomicCmpXchgInst::Init(Value *Ptr, Value *Cmp, Value *NewVal,
Tim Northovere94a5182014-03-11 10:48:52 +00001475 AtomicOrdering SuccessOrdering,
1476 AtomicOrdering FailureOrdering,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001477 SynchronizationScope SynchScope) {
1478 Op<0>() = Ptr;
1479 Op<1>() = Cmp;
1480 Op<2>() = NewVal;
Tim Northovere94a5182014-03-11 10:48:52 +00001481 setSuccessOrdering(SuccessOrdering);
1482 setFailureOrdering(FailureOrdering);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001483 setSynchScope(SynchScope);
1484
1485 assert(getOperand(0) && getOperand(1) && getOperand(2) &&
1486 "All operands must be non-null!");
1487 assert(getOperand(0)->getType()->isPointerTy() &&
1488 "Ptr must have pointer type!");
1489 assert(getOperand(1)->getType() ==
1490 cast<PointerType>(getOperand(0)->getType())->getElementType()
1491 && "Ptr must be a pointer to Cmp type!");
1492 assert(getOperand(2)->getType() ==
1493 cast<PointerType>(getOperand(0)->getType())->getElementType()
1494 && "Ptr must be a pointer to NewVal type!");
JF Bastien800f87a2016-04-06 21:19:33 +00001495 assert(SuccessOrdering != AtomicOrdering::NotAtomic &&
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001496 "AtomicCmpXchg instructions must be atomic!");
JF Bastien800f87a2016-04-06 21:19:33 +00001497 assert(FailureOrdering != AtomicOrdering::NotAtomic &&
Tim Northovere94a5182014-03-11 10:48:52 +00001498 "AtomicCmpXchg instructions must be atomic!");
JF Bastien800f87a2016-04-06 21:19:33 +00001499 assert(!isStrongerThan(FailureOrdering, SuccessOrdering) &&
1500 "AtomicCmpXchg failure argument shall be no stronger than the success "
1501 "argument");
1502 assert(FailureOrdering != AtomicOrdering::Release &&
1503 FailureOrdering != AtomicOrdering::AcquireRelease &&
Tim Northovere94a5182014-03-11 10:48:52 +00001504 "AtomicCmpXchg failure ordering cannot include release semantics");
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001505}
1506
1507AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
Tim Northovere94a5182014-03-11 10:48:52 +00001508 AtomicOrdering SuccessOrdering,
1509 AtomicOrdering FailureOrdering,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001510 SynchronizationScope SynchScope,
1511 Instruction *InsertBefore)
Tim Northover420a2162014-06-13 14:24:07 +00001512 : Instruction(
Serge Gueltone38003f2017-05-09 19:31:13 +00001513 StructType::get(Cmp->getType(), Type::getInt1Ty(Cmp->getContext())),
Tim Northover420a2162014-06-13 14:24:07 +00001514 AtomicCmpXchg, OperandTraits<AtomicCmpXchgInst>::op_begin(this),
1515 OperandTraits<AtomicCmpXchgInst>::operands(this), InsertBefore) {
Tim Northovere94a5182014-03-11 10:48:52 +00001516 Init(Ptr, Cmp, NewVal, SuccessOrdering, FailureOrdering, SynchScope);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001517}
1518
1519AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
Tim Northovere94a5182014-03-11 10:48:52 +00001520 AtomicOrdering SuccessOrdering,
1521 AtomicOrdering FailureOrdering,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001522 SynchronizationScope SynchScope,
1523 BasicBlock *InsertAtEnd)
Tim Northover420a2162014-06-13 14:24:07 +00001524 : Instruction(
Serge Gueltone38003f2017-05-09 19:31:13 +00001525 StructType::get(Cmp->getType(), Type::getInt1Ty(Cmp->getContext())),
Tim Northover420a2162014-06-13 14:24:07 +00001526 AtomicCmpXchg, OperandTraits<AtomicCmpXchgInst>::op_begin(this),
1527 OperandTraits<AtomicCmpXchgInst>::operands(this), InsertAtEnd) {
Tim Northovere94a5182014-03-11 10:48:52 +00001528 Init(Ptr, Cmp, NewVal, SuccessOrdering, FailureOrdering, SynchScope);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001529}
Tim Northover420a2162014-06-13 14:24:07 +00001530
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001531//===----------------------------------------------------------------------===//
1532// AtomicRMWInst Implementation
1533//===----------------------------------------------------------------------===//
1534
1535void AtomicRMWInst::Init(BinOp Operation, Value *Ptr, Value *Val,
1536 AtomicOrdering Ordering,
1537 SynchronizationScope SynchScope) {
1538 Op<0>() = Ptr;
1539 Op<1>() = Val;
1540 setOperation(Operation);
1541 setOrdering(Ordering);
1542 setSynchScope(SynchScope);
1543
1544 assert(getOperand(0) && getOperand(1) &&
1545 "All operands must be non-null!");
1546 assert(getOperand(0)->getType()->isPointerTy() &&
1547 "Ptr must have pointer type!");
1548 assert(getOperand(1)->getType() ==
1549 cast<PointerType>(getOperand(0)->getType())->getElementType()
1550 && "Ptr must be a pointer to Val type!");
JF Bastien800f87a2016-04-06 21:19:33 +00001551 assert(Ordering != AtomicOrdering::NotAtomic &&
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001552 "AtomicRMW instructions must be atomic!");
1553}
1554
1555AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
1556 AtomicOrdering Ordering,
1557 SynchronizationScope SynchScope,
1558 Instruction *InsertBefore)
1559 : Instruction(Val->getType(), AtomicRMW,
1560 OperandTraits<AtomicRMWInst>::op_begin(this),
1561 OperandTraits<AtomicRMWInst>::operands(this),
1562 InsertBefore) {
1563 Init(Operation, Ptr, Val, Ordering, SynchScope);
1564}
1565
1566AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
1567 AtomicOrdering Ordering,
1568 SynchronizationScope SynchScope,
1569 BasicBlock *InsertAtEnd)
1570 : Instruction(Val->getType(), AtomicRMW,
1571 OperandTraits<AtomicRMWInst>::op_begin(this),
1572 OperandTraits<AtomicRMWInst>::operands(this),
1573 InsertAtEnd) {
1574 Init(Operation, Ptr, Val, Ordering, SynchScope);
1575}
1576
1577//===----------------------------------------------------------------------===//
Eli Friedmanfee02c62011-07-25 23:16:38 +00001578// FenceInst Implementation
1579//===----------------------------------------------------------------------===//
1580
1581FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering,
1582 SynchronizationScope SynchScope,
1583 Instruction *InsertBefore)
Craig Topperc6207612014-04-09 06:08:46 +00001584 : Instruction(Type::getVoidTy(C), Fence, nullptr, 0, InsertBefore) {
Eli Friedmanfee02c62011-07-25 23:16:38 +00001585 setOrdering(Ordering);
1586 setSynchScope(SynchScope);
1587}
1588
1589FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering,
1590 SynchronizationScope SynchScope,
1591 BasicBlock *InsertAtEnd)
Craig Topperc6207612014-04-09 06:08:46 +00001592 : Instruction(Type::getVoidTy(C), Fence, nullptr, 0, InsertAtEnd) {
Eli Friedmanfee02c62011-07-25 23:16:38 +00001593 setOrdering(Ordering);
1594 setSynchScope(SynchScope);
1595}
1596
1597//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001598// GetElementPtrInst Implementation
1599//===----------------------------------------------------------------------===//
1600
Jay Foadd1b78492011-07-25 09:48:08 +00001601void GetElementPtrInst::init(Value *Ptr, ArrayRef<Value *> IdxList,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001602 const Twine &Name) {
Pete Cooperb4eede22015-06-12 17:48:10 +00001603 assert(getNumOperands() == 1 + IdxList.size() &&
1604 "NumOperands not initialized?");
Pete Coopereb31b682015-05-21 22:48:54 +00001605 Op<0>() = Ptr;
Jay Foadd1b78492011-07-25 09:48:08 +00001606 std::copy(IdxList.begin(), IdxList.end(), op_begin() + 1);
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001607 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001608}
1609
Gabor Greiff6caff662008-05-10 08:32:32 +00001610GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
David Blaikie73cf8722015-05-05 18:03:48 +00001611 : Instruction(GEPI.getType(), GetElementPtr,
1612 OperandTraits<GetElementPtrInst>::op_end(this) -
1613 GEPI.getNumOperands(),
1614 GEPI.getNumOperands()),
David Blaikief5147ef2015-06-01 03:09:34 +00001615 SourceElementType(GEPI.SourceElementType),
1616 ResultElementType(GEPI.ResultElementType) {
Jay Foadd1b78492011-07-25 09:48:08 +00001617 std::copy(GEPI.op_begin(), GEPI.op_end(), op_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001618 SubclassOptionalData = GEPI.SubclassOptionalData;
Gabor Greiff6caff662008-05-10 08:32:32 +00001619}
1620
Chris Lattner6090a422009-03-09 04:46:40 +00001621/// getIndexedType - Returns the type of the element that would be accessed with
1622/// a gep instruction with the specified parameters.
1623///
1624/// The Idxs pointer should point to a continuous piece of memory containing the
1625/// indices, either as Value* or uint64_t.
1626///
1627/// A null type is returned if the indices are invalid for the specified
1628/// pointer type.
1629///
Matthijs Kooijman04468622008-07-29 08:46:11 +00001630template <typename IndexTy>
David Blaikied288fb82015-03-30 21:41:43 +00001631static Type *getIndexedTypeInternal(Type *Agg, ArrayRef<IndexTy> IdxList) {
Chris Lattner6090a422009-03-09 04:46:40 +00001632 // Handle the special case of the empty set index set, which is always valid.
Jay Foadd1b78492011-07-25 09:48:08 +00001633 if (IdxList.empty())
Dan Gohman12fce772008-05-15 19:50:34 +00001634 return Agg;
Nadav Rotem3924cb02011-12-05 06:29:09 +00001635
Chris Lattner6090a422009-03-09 04:46:40 +00001636 // If there is at least one index, the top level type must be sized, otherwise
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001637 // it cannot be 'stepped over'.
1638 if (!Agg->isSized())
Craig Topperc6207612014-04-09 06:08:46 +00001639 return nullptr;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001640
Dan Gohman1ecaf452008-05-31 00:58:22 +00001641 unsigned CurIdx = 1;
Jay Foadd1b78492011-07-25 09:48:08 +00001642 for (; CurIdx != IdxList.size(); ++CurIdx) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001643 CompositeType *CT = dyn_cast<CompositeType>(Agg);
Craig Topperc6207612014-04-09 06:08:46 +00001644 if (!CT || CT->isPointerTy()) return nullptr;
Jay Foadd1b78492011-07-25 09:48:08 +00001645 IndexTy Index = IdxList[CurIdx];
Craig Topperc6207612014-04-09 06:08:46 +00001646 if (!CT->indexValid(Index)) return nullptr;
Dan Gohman1ecaf452008-05-31 00:58:22 +00001647 Agg = CT->getTypeAtIndex(Index);
Dan Gohman1ecaf452008-05-31 00:58:22 +00001648 }
Craig Topperc6207612014-04-09 06:08:46 +00001649 return CurIdx == IdxList.size() ? Agg : nullptr;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001650}
1651
David Blaikied288fb82015-03-30 21:41:43 +00001652Type *GetElementPtrInst::getIndexedType(Type *Ty, ArrayRef<Value *> IdxList) {
1653 return getIndexedTypeInternal(Ty, IdxList);
Matthijs Kooijman04468622008-07-29 08:46:11 +00001654}
1655
David Blaikied288fb82015-03-30 21:41:43 +00001656Type *GetElementPtrInst::getIndexedType(Type *Ty,
Jay Foadd1b78492011-07-25 09:48:08 +00001657 ArrayRef<Constant *> IdxList) {
David Blaikied288fb82015-03-30 21:41:43 +00001658 return getIndexedTypeInternal(Ty, IdxList);
Jay Foad1d4a8fe2011-01-14 08:07:43 +00001659}
1660
David Blaikied288fb82015-03-30 21:41:43 +00001661Type *GetElementPtrInst::getIndexedType(Type *Ty, ArrayRef<uint64_t> IdxList) {
1662 return getIndexedTypeInternal(Ty, IdxList);
Matthijs Kooijman04468622008-07-29 08:46:11 +00001663}
1664
Chris Lattner45f15572007-04-14 00:12:57 +00001665/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1666/// zeros. If so, the result pointer and the first operand have the same
1667/// value, just potentially different types.
1668bool GetElementPtrInst::hasAllZeroIndices() const {
1669 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1670 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1671 if (!CI->isZero()) return false;
1672 } else {
1673 return false;
1674 }
1675 }
1676 return true;
1677}
1678
Chris Lattner27058292007-04-27 20:35:56 +00001679/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1680/// constant integers. If so, the result pointer and the first operand have
1681/// a constant offset between them.
1682bool GetElementPtrInst::hasAllConstantIndices() const {
1683 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1684 if (!isa<ConstantInt>(getOperand(i)))
1685 return false;
1686 }
1687 return true;
1688}
1689
Dan Gohman1b849082009-09-07 23:54:19 +00001690void GetElementPtrInst::setIsInBounds(bool B) {
1691 cast<GEPOperator>(this)->setIsInBounds(B);
1692}
Chris Lattner45f15572007-04-14 00:12:57 +00001693
Nick Lewycky28a5f252009-09-27 21:33:04 +00001694bool GetElementPtrInst::isInBounds() const {
1695 return cast<GEPOperator>(this)->isInBounds();
1696}
1697
Chandler Carruth1e140532012-12-11 10:29:10 +00001698bool GetElementPtrInst::accumulateConstantOffset(const DataLayout &DL,
1699 APInt &Offset) const {
Chandler Carruth7ec41c72012-12-11 11:05:15 +00001700 // Delegate to the generic GEPOperator implementation.
1701 return cast<GEPOperator>(this)->accumulateConstantOffset(DL, Offset);
Chandler Carruth1e140532012-12-11 10:29:10 +00001702}
1703
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001704//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001705// ExtractElementInst Implementation
1706//===----------------------------------------------------------------------===//
1707
1708ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001709 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001710 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001711 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001712 ExtractElement,
1713 OperandTraits<ExtractElementInst>::op_begin(this),
1714 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001715 assert(isValidOperands(Val, Index) &&
1716 "Invalid extractelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001717 Op<0>() = Val;
1718 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001719 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001720}
1721
1722ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001723 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001724 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001725 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001726 ExtractElement,
1727 OperandTraits<ExtractElementInst>::op_begin(this),
1728 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001729 assert(isValidOperands(Val, Index) &&
1730 "Invalid extractelement instruction operands!");
1731
Gabor Greif2d3024d2008-05-26 21:33:52 +00001732 Op<0>() = Val;
1733 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001734 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001735}
1736
Chris Lattner54865b32006-04-08 04:05:48 +00001737bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00001738 if (!Val->getType()->isVectorTy() || !Index->getType()->isIntegerTy())
Chris Lattner54865b32006-04-08 04:05:48 +00001739 return false;
1740 return true;
1741}
1742
Robert Bocchino23004482006-01-10 19:05:34 +00001743//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001744// InsertElementInst Implementation
1745//===----------------------------------------------------------------------===//
1746
Chris Lattner54865b32006-04-08 04:05:48 +00001747InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001748 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001749 Instruction *InsertBef)
Gabor Greiff6caff662008-05-10 08:32:32 +00001750 : Instruction(Vec->getType(), InsertElement,
1751 OperandTraits<InsertElementInst>::op_begin(this),
1752 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001753 assert(isValidOperands(Vec, Elt, Index) &&
1754 "Invalid insertelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001755 Op<0>() = Vec;
1756 Op<1>() = Elt;
1757 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001758 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001759}
1760
Chris Lattner54865b32006-04-08 04:05:48 +00001761InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001762 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001763 BasicBlock *InsertAE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001764 : Instruction(Vec->getType(), InsertElement,
1765 OperandTraits<InsertElementInst>::op_begin(this),
1766 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001767 assert(isValidOperands(Vec, Elt, Index) &&
1768 "Invalid insertelement instruction operands!");
1769
Gabor Greif2d3024d2008-05-26 21:33:52 +00001770 Op<0>() = Vec;
1771 Op<1>() = Elt;
1772 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001773 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001774}
1775
Chris Lattner54865b32006-04-08 04:05:48 +00001776bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1777 const Value *Index) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001778 if (!Vec->getType()->isVectorTy())
Reid Spencer09575ba2007-02-15 03:39:18 +00001779 return false; // First operand of insertelement must be vector type.
Chris Lattner54865b32006-04-08 04:05:48 +00001780
Reid Spencerd84d35b2007-02-15 02:26:10 +00001781 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001782 return false;// Second operand of insertelement must be vector element type.
Chris Lattner54865b32006-04-08 04:05:48 +00001783
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00001784 if (!Index->getType()->isIntegerTy())
Dan Gohman4fe64de2009-06-14 23:30:43 +00001785 return false; // Third operand of insertelement must be i32.
Chris Lattner54865b32006-04-08 04:05:48 +00001786 return true;
1787}
1788
Robert Bocchinoca27f032006-01-17 20:07:22 +00001789//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001790// ShuffleVectorInst Implementation
1791//===----------------------------------------------------------------------===//
1792
1793ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001794 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001795 Instruction *InsertBefore)
Owen Anderson4056ca92009-07-29 22:17:13 +00001796: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
Mon P Wang25f01062008-11-10 04:46:22 +00001797 cast<VectorType>(Mask->getType())->getNumElements()),
1798 ShuffleVector,
1799 OperandTraits<ShuffleVectorInst>::op_begin(this),
1800 OperandTraits<ShuffleVectorInst>::operands(this),
1801 InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001802 assert(isValidOperands(V1, V2, Mask) &&
1803 "Invalid shuffle vector instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001804 Op<0>() = V1;
1805 Op<1>() = V2;
1806 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001807 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001808}
1809
1810ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001811 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001812 BasicBlock *InsertAtEnd)
Dan Gohmane5af8cd2009-08-25 23:27:45 +00001813: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
1814 cast<VectorType>(Mask->getType())->getNumElements()),
1815 ShuffleVector,
1816 OperandTraits<ShuffleVectorInst>::op_begin(this),
1817 OperandTraits<ShuffleVectorInst>::operands(this),
1818 InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001819 assert(isValidOperands(V1, V2, Mask) &&
1820 "Invalid shuffle vector instruction operands!");
1821
Gabor Greif2d3024d2008-05-26 21:33:52 +00001822 Op<0>() = V1;
1823 Op<1>() = V2;
1824 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001825 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001826}
1827
Mon P Wang25f01062008-11-10 04:46:22 +00001828bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001829 const Value *Mask) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001830 // V1 and V2 must be vectors of the same type.
Duncan Sands19d0b472010-02-16 11:11:14 +00001831 if (!V1->getType()->isVectorTy() || V1->getType() != V2->getType())
Chris Lattnerf724e342008-03-02 05:28:33 +00001832 return false;
1833
Chris Lattner1dcb6542012-01-25 23:49:49 +00001834 // Mask must be vector of i32.
Sanjay Patel8bd52282017-04-19 16:22:19 +00001835 auto *MaskTy = dyn_cast<VectorType>(Mask->getType());
Craig Topperc6207612014-04-09 06:08:46 +00001836 if (!MaskTy || !MaskTy->getElementType()->isIntegerTy(32))
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001837 return false;
Nate Begeman60a31c32010-08-13 00:16:46 +00001838
1839 // Check to see if Mask is valid.
Chris Lattner1dcb6542012-01-25 23:49:49 +00001840 if (isa<UndefValue>(Mask) || isa<ConstantAggregateZero>(Mask))
1841 return true;
1842
Sanjay Patel8bd52282017-04-19 16:22:19 +00001843 if (const auto *MV = dyn_cast<ConstantVector>(Mask)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001844 unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements();
Benjamin Kramer3ad5c962014-03-10 15:03:06 +00001845 for (Value *Op : MV->operands()) {
Sanjay Patel8bd52282017-04-19 16:22:19 +00001846 if (auto *CI = dyn_cast<ConstantInt>(Op)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001847 if (CI->uge(V1Size*2))
Nate Begeman60a31c32010-08-13 00:16:46 +00001848 return false;
Benjamin Kramer3ad5c962014-03-10 15:03:06 +00001849 } else if (!isa<UndefValue>(Op)) {
Nate Begeman60a31c32010-08-13 00:16:46 +00001850 return false;
1851 }
1852 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001853 return true;
Mon P Wang6ebf4012011-10-26 00:34:48 +00001854 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001855
Sanjay Patel8bd52282017-04-19 16:22:19 +00001856 if (const auto *CDS = dyn_cast<ConstantDataSequential>(Mask)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001857 unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements();
1858 for (unsigned i = 0, e = MaskTy->getNumElements(); i != e; ++i)
1859 if (CDS->getElementAsInteger(i) >= V1Size*2)
1860 return false;
1861 return true;
1862 }
1863
1864 // The bitcode reader can create a place holder for a forward reference
1865 // used as the shuffle mask. When this occurs, the shuffle mask will
1866 // fall into this case and fail. To avoid this error, do this bit of
1867 // ugliness to allow such a mask pass.
Sanjay Patel8bd52282017-04-19 16:22:19 +00001868 if (const auto *CE = dyn_cast<ConstantExpr>(Mask))
Chris Lattner1dcb6542012-01-25 23:49:49 +00001869 if (CE->getOpcode() == Instruction::UserOp1)
1870 return true;
1871
1872 return false;
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001873}
1874
Chris Lattnercf129702012-01-26 02:51:13 +00001875int ShuffleVectorInst::getMaskValue(Constant *Mask, unsigned i) {
1876 assert(i < Mask->getType()->getVectorNumElements() && "Index out of range");
Sanjay Patel8bd52282017-04-19 16:22:19 +00001877 if (auto *CDS = dyn_cast<ConstantDataSequential>(Mask))
Chris Lattner1dcb6542012-01-25 23:49:49 +00001878 return CDS->getElementAsInteger(i);
Chris Lattnercf129702012-01-26 02:51:13 +00001879 Constant *C = Mask->getAggregateElement(i);
Chris Lattner1dcb6542012-01-25 23:49:49 +00001880 if (isa<UndefValue>(C))
Chris Lattnerf724e342008-03-02 05:28:33 +00001881 return -1;
Chris Lattner1dcb6542012-01-25 23:49:49 +00001882 return cast<ConstantInt>(C)->getZExtValue();
Chris Lattnerf724e342008-03-02 05:28:33 +00001883}
1884
Chris Lattnercf129702012-01-26 02:51:13 +00001885void ShuffleVectorInst::getShuffleMask(Constant *Mask,
1886 SmallVectorImpl<int> &Result) {
1887 unsigned NumElts = Mask->getType()->getVectorNumElements();
Chris Lattner1dcb6542012-01-25 23:49:49 +00001888
Sanjay Patel8bd52282017-04-19 16:22:19 +00001889 if (auto *CDS = dyn_cast<ConstantDataSequential>(Mask)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001890 for (unsigned i = 0; i != NumElts; ++i)
1891 Result.push_back(CDS->getElementAsInteger(i));
1892 return;
1893 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001894 for (unsigned i = 0; i != NumElts; ++i) {
1895 Constant *C = Mask->getAggregateElement(i);
1896 Result.push_back(isa<UndefValue>(C) ? -1 :
Chris Lattner3dbad4032012-01-26 00:41:50 +00001897 cast<ConstantInt>(C)->getZExtValue());
Chris Lattner1dcb6542012-01-25 23:49:49 +00001898 }
1899}
1900
Dan Gohman12fce772008-05-15 19:50:34 +00001901//===----------------------------------------------------------------------===//
Dan Gohman0752bff2008-05-23 00:36:11 +00001902// InsertValueInst Class
1903//===----------------------------------------------------------------------===//
1904
Jay Foad57aa6362011-07-13 10:26:04 +00001905void InsertValueInst::init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
1906 const Twine &Name) {
Pete Cooperb4eede22015-06-12 17:48:10 +00001907 assert(getNumOperands() == 2 && "NumOperands not initialized?");
Jay Foad57aa6362011-07-13 10:26:04 +00001908
1909 // There's no fundamental reason why we require at least one index
1910 // (other than weirdness with &*IdxBegin being invalid; see
1911 // getelementptr's init routine for example). But there's no
1912 // present need to support it.
Eugene Zelenkod761e2c2017-05-15 21:57:41 +00001913 assert(!Idxs.empty() && "InsertValueInst must have at least one index");
Jay Foad57aa6362011-07-13 10:26:04 +00001914
1915 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs) ==
Frits van Bommel16ebe772010-12-05 20:50:26 +00001916 Val->getType() && "Inserted value must match indexed type!");
Dan Gohman1ecaf452008-05-31 00:58:22 +00001917 Op<0>() = Agg;
1918 Op<1>() = Val;
Dan Gohman0752bff2008-05-23 00:36:11 +00001919
Jay Foad57aa6362011-07-13 10:26:04 +00001920 Indices.append(Idxs.begin(), Idxs.end());
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001921 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001922}
1923
1924InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
Gabor Greife9408e62008-05-27 11:03:29 +00001925 : Instruction(IVI.getType(), InsertValue,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001926 OperandTraits<InsertValueInst>::op_begin(this), 2),
1927 Indices(IVI.Indices) {
Dan Gohmand8ca05f2008-06-17 23:25:49 +00001928 Op<0>() = IVI.getOperand(0);
1929 Op<1>() = IVI.getOperand(1);
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001930 SubclassOptionalData = IVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001931}
1932
1933//===----------------------------------------------------------------------===//
Dan Gohman12fce772008-05-15 19:50:34 +00001934// ExtractValueInst Class
1935//===----------------------------------------------------------------------===//
1936
Jay Foad57aa6362011-07-13 10:26:04 +00001937void ExtractValueInst::init(ArrayRef<unsigned> Idxs, const Twine &Name) {
Pete Cooperb4eede22015-06-12 17:48:10 +00001938 assert(getNumOperands() == 1 && "NumOperands not initialized?");
Dan Gohman0752bff2008-05-23 00:36:11 +00001939
Jay Foad57aa6362011-07-13 10:26:04 +00001940 // There's no fundamental reason why we require at least one index.
1941 // But there's no present need to support it.
Eugene Zelenkod761e2c2017-05-15 21:57:41 +00001942 assert(!Idxs.empty() && "ExtractValueInst must have at least one index");
Dan Gohman0752bff2008-05-23 00:36:11 +00001943
Jay Foad57aa6362011-07-13 10:26:04 +00001944 Indices.append(Idxs.begin(), Idxs.end());
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001945 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001946}
1947
1948ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
Gabor Greif21ba1842008-06-06 20:28:12 +00001949 : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)),
Dan Gohman1ecaf452008-05-31 00:58:22 +00001950 Indices(EVI.Indices) {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001951 SubclassOptionalData = EVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001952}
1953
Dan Gohman12fce772008-05-15 19:50:34 +00001954// getIndexedType - Returns the type of the element that would be extracted
1955// with an extractvalue instruction with the specified parameters.
1956//
1957// A null type is returned if the indices are invalid for the specified
1958// pointer type.
1959//
Chris Lattner229907c2011-07-18 04:54:35 +00001960Type *ExtractValueInst::getIndexedType(Type *Agg,
Jay Foad57aa6362011-07-13 10:26:04 +00001961 ArrayRef<unsigned> Idxs) {
Benjamin Kramer3ad5c962014-03-10 15:03:06 +00001962 for (unsigned Index : Idxs) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001963 // We can't use CompositeType::indexValid(Index) here.
1964 // indexValid() always returns true for arrays because getelementptr allows
1965 // out-of-bounds indices. Since we don't allow those for extractvalue and
1966 // insertvalue we need to check array indexing manually.
1967 // Since the only other types we can index into are struct types it's just
1968 // as easy to check those manually as well.
Chris Lattner229907c2011-07-18 04:54:35 +00001969 if (ArrayType *AT = dyn_cast<ArrayType>(Agg)) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001970 if (Index >= AT->getNumElements())
Craig Topperc6207612014-04-09 06:08:46 +00001971 return nullptr;
Chris Lattner229907c2011-07-18 04:54:35 +00001972 } else if (StructType *ST = dyn_cast<StructType>(Agg)) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001973 if (Index >= ST->getNumElements())
Craig Topperc6207612014-04-09 06:08:46 +00001974 return nullptr;
Frits van Bommel16ebe772010-12-05 20:50:26 +00001975 } else {
1976 // Not a valid type to index into.
Craig Topperc6207612014-04-09 06:08:46 +00001977 return nullptr;
Frits van Bommel16ebe772010-12-05 20:50:26 +00001978 }
1979
1980 Agg = cast<CompositeType>(Agg)->getTypeAtIndex(Index);
Dan Gohman12fce772008-05-15 19:50:34 +00001981 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001982 return const_cast<Type*>(Agg);
Dan Gohman12fce772008-05-15 19:50:34 +00001983}
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001984
1985//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001986// BinaryOperator Class
1987//===----------------------------------------------------------------------===//
1988
Chris Lattner2195fc42007-02-24 00:55:48 +00001989BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Chris Lattner229907c2011-07-18 04:54:35 +00001990 Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001991 Instruction *InsertBefore)
Dan Gohmana2414ea2010-05-03 22:44:19 +00001992 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00001993 OperandTraits<BinaryOperator>::op_begin(this),
1994 OperandTraits<BinaryOperator>::operands(this),
1995 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001996 Op<0>() = S1;
1997 Op<1>() = S2;
Dan Gohmana2414ea2010-05-03 22:44:19 +00001998 init(iType);
Chris Lattner2195fc42007-02-24 00:55:48 +00001999 setName(Name);
2000}
2001
2002BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Chris Lattner229907c2011-07-18 04:54:35 +00002003 Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00002004 BasicBlock *InsertAtEnd)
Dan Gohmana2414ea2010-05-03 22:44:19 +00002005 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00002006 OperandTraits<BinaryOperator>::op_begin(this),
2007 OperandTraits<BinaryOperator>::operands(this),
2008 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002009 Op<0>() = S1;
2010 Op<1>() = S2;
Dan Gohmana2414ea2010-05-03 22:44:19 +00002011 init(iType);
Chris Lattner2195fc42007-02-24 00:55:48 +00002012 setName(Name);
2013}
2014
Chris Lattner2195fc42007-02-24 00:55:48 +00002015void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002016 Value *LHS = getOperand(0), *RHS = getOperand(1);
Jeffrey Yasskin9b43f332010-12-23 00:58:24 +00002017 (void)LHS; (void)RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002018 assert(LHS->getType() == RHS->getType() &&
2019 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002020#ifndef NDEBUG
2021 switch (iType) {
2022 case Add: case Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00002023 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002024 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002025 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002026 assert(getType()->isIntOrIntVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00002027 "Tried to create an integer operation on a non-integer type!");
2028 break;
2029 case FAdd: case FSub:
2030 case FMul:
2031 assert(getType() == LHS->getType() &&
2032 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002033 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00002034 "Tried to create a floating-point operation on a "
2035 "non-floating-point type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002036 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002037 case UDiv:
2038 case SDiv:
2039 assert(getType() == LHS->getType() &&
2040 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00002041 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00002042 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002043 "Incorrect operand type (not integer) for S/UDIV");
2044 break;
2045 case FDiv:
2046 assert(getType() == LHS->getType() &&
2047 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002048 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002049 "Incorrect operand type (not floating point) for FDIV");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002050 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00002051 case URem:
2052 case SRem:
2053 assert(getType() == LHS->getType() &&
2054 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00002055 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00002056 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00002057 "Incorrect operand type (not integer) for S/UREM");
2058 break;
2059 case FRem:
2060 assert(getType() == LHS->getType() &&
2061 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002062 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002063 "Incorrect operand type (not floating point) for FREM");
Reid Spencer7eb55b32006-11-02 01:53:59 +00002064 break;
Reid Spencer2341c222007-02-02 02:16:23 +00002065 case Shl:
2066 case LShr:
2067 case AShr:
2068 assert(getType() == LHS->getType() &&
2069 "Shift operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002070 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00002071 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00002072 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Nate Begemanfecbc8c2008-07-29 15:49:41 +00002073 "Tried to create a shift operation on a non-integral type!");
Reid Spencer2341c222007-02-02 02:16:23 +00002074 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002075 case And: case Or:
2076 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002077 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002078 "Logical operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002079 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00002080 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00002081 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00002082 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002083 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002084 default:
2085 break;
2086 }
2087#endif
2088}
2089
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002090BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002091 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002092 Instruction *InsertBefore) {
2093 assert(S1->getType() == S2->getType() &&
2094 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00002095 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002096}
2097
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002098BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002099 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002100 BasicBlock *InsertAtEnd) {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002101 BinaryOperator *Res = Create(Op, S1, S2, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002102 InsertAtEnd->getInstList().push_back(Res);
2103 return Res;
2104}
2105
Dan Gohman5476cfd2009-08-12 16:23:25 +00002106BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002107 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002108 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00002109 return new BinaryOperator(Instruction::Sub,
2110 zero, Op,
2111 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002112}
2113
Dan Gohman5476cfd2009-08-12 16:23:25 +00002114BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002115 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002116 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00002117 return new BinaryOperator(Instruction::Sub,
2118 zero, Op,
2119 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002120}
2121
Dan Gohman4ab44202009-12-18 02:58:50 +00002122BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
2123 Instruction *InsertBefore) {
2124 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2125 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertBefore);
2126}
2127
2128BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
2129 BasicBlock *InsertAtEnd) {
2130 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2131 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertAtEnd);
2132}
2133
Duncan Sandsfa5f5962010-02-02 12:53:04 +00002134BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
2135 Instruction *InsertBefore) {
2136 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2137 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertBefore);
2138}
2139
2140BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
2141 BasicBlock *InsertAtEnd) {
2142 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2143 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertAtEnd);
2144}
2145
Dan Gohman5476cfd2009-08-12 16:23:25 +00002146BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00002147 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002148 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Chris Lattner47a86bd2012-01-25 06:02:56 +00002149 return new BinaryOperator(Instruction::FSub, zero, Op,
Dan Gohmana5b96452009-06-04 22:49:04 +00002150 Op->getType(), Name, InsertBefore);
2151}
2152
Dan Gohman5476cfd2009-08-12 16:23:25 +00002153BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00002154 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002155 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Chris Lattner47a86bd2012-01-25 06:02:56 +00002156 return new BinaryOperator(Instruction::FSub, zero, Op,
Dan Gohmana5b96452009-06-04 22:49:04 +00002157 Op->getType(), Name, InsertAtEnd);
2158}
2159
Dan Gohman5476cfd2009-08-12 16:23:25 +00002160BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002161 Instruction *InsertBefore) {
Chris Lattner47a86bd2012-01-25 06:02:56 +00002162 Constant *C = Constant::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00002163 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002164 Op->getType(), Name, InsertBefore);
2165}
2166
Dan Gohman5476cfd2009-08-12 16:23:25 +00002167BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002168 BasicBlock *InsertAtEnd) {
Chris Lattner47a86bd2012-01-25 06:02:56 +00002169 Constant *AllOnes = Constant::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00002170 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002171 Op->getType(), Name, InsertAtEnd);
2172}
2173
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002174// isConstantAllOnes - Helper function for several functions below
2175static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner0256be92012-01-27 03:08:05 +00002176 if (const Constant *C = dyn_cast<Constant>(V))
2177 return C->isAllOnesValue();
Chris Lattner1edec382007-06-15 06:04:24 +00002178 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002179}
2180
Owen Andersonbb2501b2009-07-13 22:18:28 +00002181bool BinaryOperator::isNeg(const Value *V) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002182 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
2183 if (Bop->getOpcode() == Instruction::Sub)
Ana Pazosb3596022016-02-03 21:34:39 +00002184 if (Constant *C = dyn_cast<Constant>(Bop->getOperand(0)))
Owen Andersonbb2501b2009-07-13 22:18:28 +00002185 return C->isNegativeZeroValue();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002186 return false;
2187}
2188
Shuxin Yangf0537ab2013-01-09 00:13:41 +00002189bool BinaryOperator::isFNeg(const Value *V, bool IgnoreZeroSign) {
Dan Gohmana5b96452009-06-04 22:49:04 +00002190 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
2191 if (Bop->getOpcode() == Instruction::FSub)
Ana Pazosb3596022016-02-03 21:34:39 +00002192 if (Constant *C = dyn_cast<Constant>(Bop->getOperand(0))) {
Shuxin Yangf0537ab2013-01-09 00:13:41 +00002193 if (!IgnoreZeroSign)
2194 IgnoreZeroSign = cast<Instruction>(V)->hasNoSignedZeros();
2195 return !IgnoreZeroSign ? C->isNegativeZeroValue() : C->isZeroValue();
2196 }
Dan Gohmana5b96452009-06-04 22:49:04 +00002197 return false;
2198}
2199
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002200bool BinaryOperator::isNot(const Value *V) {
2201 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
2202 return (Bop->getOpcode() == Instruction::Xor &&
2203 (isConstantAllOnes(Bop->getOperand(1)) ||
2204 isConstantAllOnes(Bop->getOperand(0))));
2205 return false;
2206}
2207
Chris Lattner2c7d1772005-04-24 07:28:37 +00002208Value *BinaryOperator::getNegArgument(Value *BinOp) {
Chris Lattner2c7d1772005-04-24 07:28:37 +00002209 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002210}
2211
Chris Lattner2c7d1772005-04-24 07:28:37 +00002212const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
2213 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002214}
2215
Dan Gohmana5b96452009-06-04 22:49:04 +00002216Value *BinaryOperator::getFNegArgument(Value *BinOp) {
Dan Gohmana5b96452009-06-04 22:49:04 +00002217 return cast<BinaryOperator>(BinOp)->getOperand(1);
2218}
2219
2220const Value *BinaryOperator::getFNegArgument(const Value *BinOp) {
2221 return getFNegArgument(const_cast<Value*>(BinOp));
2222}
2223
Chris Lattner2c7d1772005-04-24 07:28:37 +00002224Value *BinaryOperator::getNotArgument(Value *BinOp) {
2225 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
2226 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
2227 Value *Op0 = BO->getOperand(0);
2228 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002229 if (isConstantAllOnes(Op0)) return Op1;
2230
2231 assert(isConstantAllOnes(Op1));
2232 return Op0;
2233}
2234
Chris Lattner2c7d1772005-04-24 07:28:37 +00002235const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
2236 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002237}
2238
Sanjay Patel4ce99d42016-11-16 18:09:44 +00002239// Exchange the two operands to this instruction. This instruction is safe to
2240// use on any binary instruction and does not modify the semantics of the
2241// instruction. If the instruction is order-dependent (SetLT f.e.), the opcode
2242// is changed.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002243bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00002244 if (!isCommutative())
2245 return true; // Can't commute operands
Gabor Greif5ef74042008-05-13 22:51:52 +00002246 Op<0>().swap(Op<1>());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002247 return false;
2248}
2249
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002250//===----------------------------------------------------------------------===//
Duncan Sands05f4df82012-04-16 16:28:59 +00002251// FPMathOperator Class
2252//===----------------------------------------------------------------------===//
2253
Duncan Sands05f4df82012-04-16 16:28:59 +00002254float FPMathOperator::getFPAccuracy() const {
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +00002255 const MDNode *MD =
2256 cast<Instruction>(this)->getMetadata(LLVMContext::MD_fpmath);
Duncan Sands05f4df82012-04-16 16:28:59 +00002257 if (!MD)
2258 return 0.0;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002259 ConstantFP *Accuracy = mdconst::extract<ConstantFP>(MD->getOperand(0));
Duncan Sands9af62982012-04-16 19:39:33 +00002260 return Accuracy->getValueAPF().convertToFloat();
Duncan Sands05f4df82012-04-16 16:28:59 +00002261}
2262
Duncan Sands05f4df82012-04-16 16:28:59 +00002263//===----------------------------------------------------------------------===//
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002264// CastInst Class
2265//===----------------------------------------------------------------------===//
2266
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002267// Just determine if this cast only deals with integral->integral conversion.
2268bool CastInst::isIntegerCast() const {
2269 switch (getOpcode()) {
2270 default: return false;
2271 case Instruction::ZExt:
2272 case Instruction::SExt:
2273 case Instruction::Trunc:
2274 return true;
2275 case Instruction::BitCast:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002276 return getOperand(0)->getType()->isIntegerTy() &&
2277 getType()->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002278 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002279}
2280
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002281bool CastInst::isLosslessCast() const {
2282 // Only BitCast can be lossless, exit fast if we're not BitCast
2283 if (getOpcode() != Instruction::BitCast)
2284 return false;
2285
2286 // Identity cast is always lossless
Ana Pazosb3596022016-02-03 21:34:39 +00002287 Type *SrcTy = getOperand(0)->getType();
2288 Type *DstTy = getType();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002289 if (SrcTy == DstTy)
2290 return true;
2291
Reid Spencer8d9336d2006-12-31 05:26:44 +00002292 // Pointer to pointer is always lossless.
Duncan Sands19d0b472010-02-16 11:11:14 +00002293 if (SrcTy->isPointerTy())
2294 return DstTy->isPointerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002295 return false; // Other types have no identity values
2296}
2297
2298/// This function determines if the CastInst does not require any bits to be
2299/// changed in order to effect the cast. Essentially, it identifies cases where
2300/// no code gen is necessary for the cast, hence the name no-op cast. For
2301/// example, the following are all no-op casts:
Dan Gohmane9bc2ba2008-05-12 16:34:30 +00002302/// # bitcast i32* %x to i8*
2303/// # bitcast <2 x i32> %x to <4 x i16>
2304/// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002305/// @brief Determine if the described cast is a no-op.
2306bool CastInst::isNoopCast(Instruction::CastOps Opcode,
Chris Lattner229907c2011-07-18 04:54:35 +00002307 Type *SrcTy,
2308 Type *DestTy,
2309 Type *IntPtrTy) {
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002310 switch (Opcode) {
Craig Topperc514b542012-02-05 22:14:15 +00002311 default: llvm_unreachable("Invalid CastOp");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002312 case Instruction::Trunc:
2313 case Instruction::ZExt:
2314 case Instruction::SExt:
2315 case Instruction::FPTrunc:
2316 case Instruction::FPExt:
2317 case Instruction::UIToFP:
2318 case Instruction::SIToFP:
2319 case Instruction::FPToUI:
2320 case Instruction::FPToSI:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002321 case Instruction::AddrSpaceCast:
2322 // TODO: Target informations may give a more accurate answer here.
2323 return false;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002324 case Instruction::BitCast:
2325 return true; // BitCast never modifies bits.
2326 case Instruction::PtrToInt:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002327 return IntPtrTy->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002328 DestTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002329 case Instruction::IntToPtr:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002330 return IntPtrTy->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002331 SrcTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002332 }
2333}
2334
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002335/// @brief Determine if a cast is a no-op.
Chris Lattner229907c2011-07-18 04:54:35 +00002336bool CastInst::isNoopCast(Type *IntPtrTy) const {
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002337 return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), IntPtrTy);
2338}
2339
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002340bool CastInst::isNoopCast(const DataLayout &DL) const {
Craig Topperc6207612014-04-09 06:08:46 +00002341 Type *PtrOpTy = nullptr;
Matt Arsenaulta236ea52014-03-06 17:33:55 +00002342 if (getOpcode() == Instruction::PtrToInt)
2343 PtrOpTy = getOperand(0)->getType();
2344 else if (getOpcode() == Instruction::IntToPtr)
2345 PtrOpTy = getType();
2346
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002347 Type *IntPtrTy =
2348 PtrOpTy ? DL.getIntPtrType(PtrOpTy) : DL.getIntPtrType(getContext(), 0);
Matt Arsenaulta236ea52014-03-06 17:33:55 +00002349
2350 return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), IntPtrTy);
2351}
2352
2353/// This function determines if a pair of casts can be eliminated and what
2354/// opcode should be used in the elimination. This assumes that there are two
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002355/// instructions like this:
2356/// * %F = firstOpcode SrcTy %x to MidTy
2357/// * %S = secondOpcode MidTy %F to DstTy
2358/// The function returns a resultOpcode so these two casts can be replaced with:
2359/// * %Replacement = resultOpcode %SrcTy %x to DstTy
Sanjay Patel4dad27e2015-12-11 18:12:01 +00002360/// If no such cast is permitted, the function returns 0.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002361unsigned CastInst::isEliminableCastPair(
2362 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
Duncan Sandse2395dc2012-10-30 16:03:32 +00002363 Type *SrcTy, Type *MidTy, Type *DstTy, Type *SrcIntPtrTy, Type *MidIntPtrTy,
2364 Type *DstIntPtrTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002365 // Define the 144 possibilities for these two cast instructions. The values
2366 // in this matrix determine what to do in a given situation and select the
2367 // case in the switch below. The rows correspond to firstOp, the columns
Sanjay Patel4dad27e2015-12-11 18:12:01 +00002368 // correspond to secondOp. In looking at the table below, keep in mind
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002369 // the following cast properties:
2370 //
2371 // Size Compare Source Destination
2372 // Operator Src ? Size Type Sign Type Sign
2373 // -------- ------------ ------------------- ---------------------
2374 // TRUNC > Integer Any Integral Any
2375 // ZEXT < Integral Unsigned Integer Any
2376 // SEXT < Integral Signed Integer Any
2377 // FPTOUI n/a FloatPt n/a Integral Unsigned
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002378 // FPTOSI n/a FloatPt n/a Integral Signed
2379 // UITOFP n/a Integral Unsigned FloatPt n/a
2380 // SITOFP n/a Integral Signed FloatPt n/a
2381 // FPTRUNC > FloatPt n/a FloatPt n/a
2382 // FPEXT < FloatPt n/a FloatPt n/a
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002383 // PTRTOINT n/a Pointer n/a Integral Unsigned
2384 // INTTOPTR n/a Integral Unsigned Pointer n/a
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002385 // BITCAST = FirstClass n/a FirstClass n/a
2386 // ADDRSPCST n/a Pointer n/a Pointer n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00002387 //
2388 // NOTE: some transforms are safe, but we consider them to be non-profitable.
Dan Gohman4fe64de2009-06-14 23:30:43 +00002389 // For example, we could merge "fptoui double to i32" + "zext i32 to i64",
2390 // into "fptoui double to i64", but this loses information about the range
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002391 // of the produced value (we no longer know the top-part is all zeros).
Chris Lattner6f6b4972006-12-05 23:43:59 +00002392 // Further this conversion is often much more expensive for typical hardware,
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002393 // and causes issues when building libgcc. We disallow fptosi+sext for the
Chris Lattner6f6b4972006-12-05 23:43:59 +00002394 // same reason.
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002395 const unsigned numCastOps =
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002396 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
2397 static const uint8_t CastResults[numCastOps][numCastOps] = {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002398 // T F F U S F F P I B A -+
2399 // R Z S P P I I T P 2 N T S |
2400 // U E E 2 2 2 2 R E I T C C +- secondOp
2401 // N X X U S F F N X N 2 V V |
2402 // C T T I I P P C T T P T T -+
2403 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // Trunc -+
Fiona Glaser0d41db12015-04-21 00:05:41 +00002404 { 8, 1, 9,99,99, 2,17,99,99,99, 2, 3, 0}, // ZExt |
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002405 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3, 0}, // SExt |
2406 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // FPToUI |
2407 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // FPToSI |
2408 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // UIToFP +- firstOp
2409 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // SIToFP |
Ahmed Bougacha0ea9d1e2015-05-29 00:04:30 +00002410 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // FPTrunc |
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002411 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4, 0}, // FPExt |
2412 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3, 0}, // PtrToInt |
2413 { 99,99,99,99,99,99,99,99,99,11,99,15, 0}, // IntToPtr |
2414 { 5, 5, 5, 6, 6, 5, 5, 6, 6,16, 5, 1,14}, // BitCast |
2415 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,13,12}, // AddrSpaceCast -+
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002416 };
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002417
Sanjay Patel93f55dd2015-12-12 00:33:36 +00002418 // TODO: This logic could be encoded into the table above and handled in the
2419 // switch below.
Chris Lattner25eea4d2010-07-12 01:19:22 +00002420 // If either of the casts are a bitcast from scalar to vector, disallow the
Sanjay Patel93f55dd2015-12-12 00:33:36 +00002421 // merging. However, any pair of bitcasts are allowed.
2422 bool IsFirstBitcast = (firstOp == Instruction::BitCast);
2423 bool IsSecondBitcast = (secondOp == Instruction::BitCast);
2424 bool AreBothBitcasts = IsFirstBitcast && IsSecondBitcast;
Nadav Rotem5fc81ff2011-08-29 19:58:36 +00002425
Sanjay Patel93f55dd2015-12-12 00:33:36 +00002426 // Check if any of the casts convert scalars <-> vectors.
2427 if ((IsFirstBitcast && isa<VectorType>(SrcTy) != isa<VectorType>(MidTy)) ||
2428 (IsSecondBitcast && isa<VectorType>(MidTy) != isa<VectorType>(DstTy)))
2429 if (!AreBothBitcasts)
2430 return 0;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002431
2432 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
2433 [secondOp-Instruction::CastOpsBegin];
2434 switch (ElimCase) {
2435 case 0:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002436 // Categorically disallowed.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002437 return 0;
2438 case 1:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002439 // Allowed, use first cast's opcode.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002440 return firstOp;
2441 case 2:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002442 // Allowed, use second cast's opcode.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002443 return secondOp;
2444 case 3:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002445 // No-op cast in second op implies firstOp as long as the DestTy
Mon P Wange04b4562010-01-23 04:35:57 +00002446 // is integer and we are not converting between a vector and a
Alp Tokerf907b892013-12-05 05:44:44 +00002447 // non-vector type.
Duncan Sands19d0b472010-02-16 11:11:14 +00002448 if (!SrcTy->isVectorTy() && DstTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002449 return firstOp;
2450 return 0;
2451 case 4:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002452 // No-op cast in second op implies firstOp as long as the DestTy
Chris Lattner531732b2010-01-23 04:42:42 +00002453 // is floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002454 if (DstTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002455 return firstOp;
2456 return 0;
2457 case 5:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002458 // No-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002459 // is an integer.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002460 if (SrcTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002461 return secondOp;
2462 return 0;
2463 case 6:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002464 // No-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002465 // is a floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002466 if (SrcTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002467 return secondOp;
2468 return 0;
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002469 case 7: {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002470 // Cannot simplify if address spaces are different!
2471 if (SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace())
2472 return 0;
2473
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002474 unsigned MidSize = MidTy->getScalarSizeInBits();
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002475 // We can still fold this without knowing the actual sizes as long we
2476 // know that the intermediate pointer is the largest possible
2477 // pointer size.
2478 // FIXME: Is this always true?
2479 if (MidSize == 64)
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002480 return Instruction::BitCast;
2481
2482 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size.
Duncan Sandse2395dc2012-10-30 16:03:32 +00002483 if (!SrcIntPtrTy || DstIntPtrTy != SrcIntPtrTy)
Dan Gohman9413de12009-07-21 23:19:40 +00002484 return 0;
Duncan Sandse2395dc2012-10-30 16:03:32 +00002485 unsigned PtrSize = SrcIntPtrTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002486 if (MidSize >= PtrSize)
2487 return Instruction::BitCast;
2488 return 0;
2489 }
2490 case 8: {
2491 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
2492 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
2493 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002494 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2495 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002496 if (SrcSize == DstSize)
2497 return Instruction::BitCast;
2498 else if (SrcSize < DstSize)
2499 return firstOp;
2500 return secondOp;
2501 }
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002502 case 9:
2503 // zext, sext -> zext, because sext can't sign extend after zext
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002504 return Instruction::ZExt;
2505 case 10:
2506 // fpext followed by ftrunc is allowed if the bit size returned to is
2507 // the same as the original, in which case its just a bitcast
2508 if (SrcTy == DstTy)
2509 return Instruction::BitCast;
2510 return 0; // If the types are not the same we can't eliminate it.
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002511 case 11: {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002512 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
Duncan Sandse2395dc2012-10-30 16:03:32 +00002513 if (!MidIntPtrTy)
Dan Gohman9413de12009-07-21 23:19:40 +00002514 return 0;
Duncan Sandse2395dc2012-10-30 16:03:32 +00002515 unsigned PtrSize = MidIntPtrTy->getScalarSizeInBits();
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002516 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2517 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002518 if (SrcSize <= PtrSize && SrcSize == DstSize)
2519 return Instruction::BitCast;
2520 return 0;
2521 }
Eugene Zelenkod761e2c2017-05-15 21:57:41 +00002522 case 12:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002523 // addrspacecast, addrspacecast -> bitcast, if SrcAS == DstAS
2524 // addrspacecast, addrspacecast -> addrspacecast, if SrcAS != DstAS
2525 if (SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace())
2526 return Instruction::AddrSpaceCast;
2527 return Instruction::BitCast;
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002528 case 13:
2529 // FIXME: this state can be merged with (1), but the following assert
2530 // is useful to check the correcteness of the sequence due to semantic
2531 // change of bitcast.
2532 assert(
2533 SrcTy->isPtrOrPtrVectorTy() &&
2534 MidTy->isPtrOrPtrVectorTy() &&
2535 DstTy->isPtrOrPtrVectorTy() &&
2536 SrcTy->getPointerAddressSpace() != MidTy->getPointerAddressSpace() &&
2537 MidTy->getPointerAddressSpace() == DstTy->getPointerAddressSpace() &&
2538 "Illegal addrspacecast, bitcast sequence!");
2539 // Allowed, use first cast's opcode
2540 return firstOp;
2541 case 14:
Jingyue Wu77145d92014-06-06 21:52:55 +00002542 // bitcast, addrspacecast -> addrspacecast if the element type of
2543 // bitcast's source is the same as that of addrspacecast's destination.
Peter Collingbourne9d5fd4d2016-11-13 06:58:45 +00002544 if (SrcTy->getScalarType()->getPointerElementType() ==
2545 DstTy->getScalarType()->getPointerElementType())
Jingyue Wu77145d92014-06-06 21:52:55 +00002546 return Instruction::AddrSpaceCast;
2547 return 0;
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002548 case 15:
2549 // FIXME: this state can be merged with (1), but the following assert
2550 // is useful to check the correcteness of the sequence due to semantic
2551 // change of bitcast.
2552 assert(
2553 SrcTy->isIntOrIntVectorTy() &&
2554 MidTy->isPtrOrPtrVectorTy() &&
2555 DstTy->isPtrOrPtrVectorTy() &&
2556 MidTy->getPointerAddressSpace() == DstTy->getPointerAddressSpace() &&
2557 "Illegal inttoptr, bitcast sequence!");
2558 // Allowed, use first cast's opcode
2559 return firstOp;
2560 case 16:
2561 // FIXME: this state can be merged with (2), but the following assert
2562 // is useful to check the correcteness of the sequence due to semantic
2563 // change of bitcast.
2564 assert(
2565 SrcTy->isPtrOrPtrVectorTy() &&
2566 MidTy->isPtrOrPtrVectorTy() &&
2567 DstTy->isIntOrIntVectorTy() &&
2568 SrcTy->getPointerAddressSpace() == MidTy->getPointerAddressSpace() &&
2569 "Illegal bitcast, ptrtoint sequence!");
2570 // Allowed, use second cast's opcode
2571 return secondOp;
Fiona Glaser0d41db12015-04-21 00:05:41 +00002572 case 17:
2573 // (sitofp (zext x)) -> (uitofp x)
2574 return Instruction::UIToFP;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002575 case 99:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002576 // Cast combination can't happen (error in input). This is for all cases
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002577 // where the MidTy is not the same for the two cast instructions.
Craig Topperc514b542012-02-05 22:14:15 +00002578 llvm_unreachable("Invalid Cast Combination");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002579 default:
Craig Topperc514b542012-02-05 22:14:15 +00002580 llvm_unreachable("Error in CastResults table!!!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002581 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002582}
2583
Chris Lattner229907c2011-07-18 04:54:35 +00002584CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002585 const Twine &Name, Instruction *InsertBefore) {
Duncan Sands7f646562011-05-18 09:21:57 +00002586 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002587 // Construct and return the appropriate CastInst subclass
2588 switch (op) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002589 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
2590 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
2591 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
2592 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
2593 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
2594 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
2595 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
2596 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
2597 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
2598 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
2599 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
2600 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
2601 case AddrSpaceCast: return new AddrSpaceCastInst (S, Ty, Name, InsertBefore);
2602 default: llvm_unreachable("Invalid opcode provided");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002603 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002604}
2605
Chris Lattner229907c2011-07-18 04:54:35 +00002606CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002607 const Twine &Name, BasicBlock *InsertAtEnd) {
Duncan Sands7f646562011-05-18 09:21:57 +00002608 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002609 // Construct and return the appropriate CastInst subclass
2610 switch (op) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002611 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
2612 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
2613 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
2614 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
2615 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
2616 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
2617 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
2618 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
2619 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
2620 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
2621 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
2622 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
2623 case AddrSpaceCast: return new AddrSpaceCastInst (S, Ty, Name, InsertAtEnd);
2624 default: llvm_unreachable("Invalid opcode provided");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002625 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002626}
2627
Chris Lattner229907c2011-07-18 04:54:35 +00002628CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002629 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002630 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002631 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002632 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2633 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002634}
2635
Chris Lattner229907c2011-07-18 04:54:35 +00002636CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002637 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002638 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002639 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002640 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2641 return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002642}
2643
Chris Lattner229907c2011-07-18 04:54:35 +00002644CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002645 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002646 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002647 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002648 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2649 return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002650}
2651
Chris Lattner229907c2011-07-18 04:54:35 +00002652CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002653 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002654 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002655 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002656 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2657 return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002658}
2659
Chris Lattner229907c2011-07-18 04:54:35 +00002660CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002661 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002662 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002663 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002664 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2665 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002666}
2667
Chris Lattner229907c2011-07-18 04:54:35 +00002668CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002669 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002670 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002671 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002672 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2673 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002674}
2675
Chris Lattner229907c2011-07-18 04:54:35 +00002676CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002677 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002678 BasicBlock *InsertAtEnd) {
Matt Arsenault065ced92013-07-31 00:17:33 +00002679 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2680 assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) &&
2681 "Invalid cast");
2682 assert(Ty->isVectorTy() == S->getType()->isVectorTy() && "Invalid cast");
Richard Trieu8dc43232013-07-31 04:07:28 +00002683 assert((!Ty->isVectorTy() ||
2684 Ty->getVectorNumElements() == S->getType()->getVectorNumElements()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002685 "Invalid cast");
2686
Matt Arsenault065ced92013-07-31 00:17:33 +00002687 if (Ty->isIntOrIntVectorTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002688 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002689
Matt Arsenault740980e2014-07-14 17:24:35 +00002690 return CreatePointerBitCastOrAddrSpaceCast(S, Ty, Name, InsertAtEnd);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002691}
2692
2693/// @brief Create a BitCast or a PtrToInt cast instruction
Matt Arsenault065ced92013-07-31 00:17:33 +00002694CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
2695 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002696 Instruction *InsertBefore) {
Evgeniy Stepanovc9bd35b2013-01-15 16:43:00 +00002697 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2698 assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002699 "Invalid cast");
Matt Arsenault065ced92013-07-31 00:17:33 +00002700 assert(Ty->isVectorTy() == S->getType()->isVectorTy() && "Invalid cast");
Richard Trieu8dc43232013-07-31 04:07:28 +00002701 assert((!Ty->isVectorTy() ||
2702 Ty->getVectorNumElements() == S->getType()->getVectorNumElements()) &&
Matt Arsenault065ced92013-07-31 00:17:33 +00002703 "Invalid cast");
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002704
Evgeniy Stepanovc9bd35b2013-01-15 16:43:00 +00002705 if (Ty->isIntOrIntVectorTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002706 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002707
Matt Arsenault740980e2014-07-14 17:24:35 +00002708 return CreatePointerBitCastOrAddrSpaceCast(S, Ty, Name, InsertBefore);
2709}
2710
2711CastInst *CastInst::CreatePointerBitCastOrAddrSpaceCast(
2712 Value *S, Type *Ty,
2713 const Twine &Name,
2714 BasicBlock *InsertAtEnd) {
2715 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2716 assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast");
2717
2718 if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace())
2719 return Create(Instruction::AddrSpaceCast, S, Ty, Name, InsertAtEnd);
2720
2721 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2722}
2723
2724CastInst *CastInst::CreatePointerBitCastOrAddrSpaceCast(
2725 Value *S, Type *Ty,
2726 const Twine &Name,
2727 Instruction *InsertBefore) {
2728 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2729 assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast");
2730
2731 if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace())
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002732 return Create(Instruction::AddrSpaceCast, S, Ty, Name, InsertBefore);
2733
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002734 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002735}
2736
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002737CastInst *CastInst::CreateBitOrPointerCast(Value *S, Type *Ty,
2738 const Twine &Name,
2739 Instruction *InsertBefore) {
2740 if (S->getType()->isPointerTy() && Ty->isIntegerTy())
2741 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
2742 if (S->getType()->isIntegerTy() && Ty->isPointerTy())
2743 return Create(Instruction::IntToPtr, S, Ty, Name, InsertBefore);
2744
2745 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2746}
2747
Matt Arsenault740980e2014-07-14 17:24:35 +00002748CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002749 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002750 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002751 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Chris Lattner5370ae72010-01-10 20:21:42 +00002752 "Invalid integer cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002753 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2754 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002755 Instruction::CastOps opcode =
2756 (SrcBits == DstBits ? Instruction::BitCast :
2757 (SrcBits > DstBits ? Instruction::Trunc :
2758 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002759 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002760}
2761
Chris Lattner229907c2011-07-18 04:54:35 +00002762CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002763 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002764 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002765 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002766 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002767 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2768 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002769 Instruction::CastOps opcode =
2770 (SrcBits == DstBits ? Instruction::BitCast :
2771 (SrcBits > DstBits ? Instruction::Trunc :
2772 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002773 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002774}
2775
Chris Lattner229907c2011-07-18 04:54:35 +00002776CastInst *CastInst::CreateFPCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002777 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002778 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002779 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002780 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002781 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2782 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002783 Instruction::CastOps opcode =
2784 (SrcBits == DstBits ? Instruction::BitCast :
2785 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002786 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002787}
2788
Chris Lattner229907c2011-07-18 04:54:35 +00002789CastInst *CastInst::CreateFPCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002790 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002791 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002792 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002793 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002794 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2795 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002796 Instruction::CastOps opcode =
2797 (SrcBits == DstBits ? Instruction::BitCast :
2798 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002799 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002800}
2801
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002802// Check whether it is valid to call getCastOpcode for these types.
2803// This routine must be kept in sync with getCastOpcode.
2804bool CastInst::isCastable(Type *SrcTy, Type *DestTy) {
2805 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2806 return false;
2807
2808 if (SrcTy == DestTy)
2809 return true;
2810
2811 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
2812 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
2813 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2814 // An element by element cast. Valid if casting the elements is valid.
2815 SrcTy = SrcVecTy->getElementType();
2816 DestTy = DestVecTy->getElementType();
2817 }
2818
2819 // Get the bit sizes, we'll need these
2820 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2821 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
2822
2823 // Run through the possibilities ...
2824 if (DestTy->isIntegerTy()) { // Casting to integral
David Blaikie9965c5a2015-03-23 19:51:23 +00002825 if (SrcTy->isIntegerTy()) // Casting from integral
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002826 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002827 if (SrcTy->isFloatingPointTy()) // Casting from floating pt
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002828 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002829 if (SrcTy->isVectorTy()) // Casting from vector
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002830 return DestBits == SrcBits;
David Blaikie9965c5a2015-03-23 19:51:23 +00002831 // Casting from something else
2832 return SrcTy->isPointerTy();
2833 }
2834 if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2835 if (SrcTy->isIntegerTy()) // Casting from integral
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002836 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002837 if (SrcTy->isFloatingPointTy()) // Casting from floating pt
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002838 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002839 if (SrcTy->isVectorTy()) // Casting from vector
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002840 return DestBits == SrcBits;
David Blaikie9965c5a2015-03-23 19:51:23 +00002841 // Casting from something else
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002842 return false;
2843 }
David Blaikie9965c5a2015-03-23 19:51:23 +00002844 if (DestTy->isVectorTy()) // Casting to vector
2845 return DestBits == SrcBits;
2846 if (DestTy->isPointerTy()) { // Casting to pointer
2847 if (SrcTy->isPointerTy()) // Casting from pointer
2848 return true;
2849 return SrcTy->isIntegerTy(); // Casting from integral
2850 }
2851 if (DestTy->isX86_MMXTy()) {
2852 if (SrcTy->isVectorTy())
2853 return DestBits == SrcBits; // 64-bit vector to MMX
2854 return false;
2855 } // Casting to something else
2856 return false;
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002857}
2858
Matt Arsenaultcacbb232013-07-30 20:45:05 +00002859bool CastInst::isBitCastable(Type *SrcTy, Type *DestTy) {
2860 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2861 return false;
2862
2863 if (SrcTy == DestTy)
2864 return true;
2865
2866 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) {
2867 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy)) {
2868 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2869 // An element by element cast. Valid if casting the elements is valid.
2870 SrcTy = SrcVecTy->getElementType();
2871 DestTy = DestVecTy->getElementType();
2872 }
2873 }
2874 }
2875
2876 if (PointerType *DestPtrTy = dyn_cast<PointerType>(DestTy)) {
2877 if (PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy)) {
2878 return SrcPtrTy->getAddressSpace() == DestPtrTy->getAddressSpace();
2879 }
2880 }
2881
2882 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2883 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
2884
2885 // Could still have vectors of pointers if the number of elements doesn't
2886 // match
2887 if (SrcBits == 0 || DestBits == 0)
2888 return false;
2889
2890 if (SrcBits != DestBits)
2891 return false;
2892
2893 if (DestTy->isX86_MMXTy() || SrcTy->isX86_MMXTy())
2894 return false;
2895
2896 return true;
2897}
2898
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002899bool CastInst::isBitOrNoopPointerCastable(Type *SrcTy, Type *DestTy,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002900 const DataLayout &DL) {
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002901 if (auto *PtrTy = dyn_cast<PointerType>(SrcTy))
2902 if (auto *IntTy = dyn_cast<IntegerType>(DestTy))
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002903 return IntTy->getBitWidth() == DL.getPointerTypeSizeInBits(PtrTy);
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002904 if (auto *PtrTy = dyn_cast<PointerType>(DestTy))
2905 if (auto *IntTy = dyn_cast<IntegerType>(SrcTy))
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002906 return IntTy->getBitWidth() == DL.getPointerTypeSizeInBits(PtrTy);
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002907
2908 return isBitCastable(SrcTy, DestTy);
2909}
2910
Matt Arsenaultcacbb232013-07-30 20:45:05 +00002911// Provide a way to get a "cast" where the cast opcode is inferred from the
2912// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002913// logic in the castIsValid function below. This axiom should hold:
2914// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2915// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002916// casting opcode for the arguments passed to it.
Duncan Sands55e50902008-01-06 10:12:28 +00002917// This routine must be kept in sync with isCastable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002918Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00002919CastInst::getCastOpcode(
Chris Lattner229907c2011-07-18 04:54:35 +00002920 const Value *Src, bool SrcIsSigned, Type *DestTy, bool DestIsSigned) {
2921 Type *SrcTy = Src->getType();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002922
Duncan Sands55e50902008-01-06 10:12:28 +00002923 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
2924 "Only first class types are castable!");
2925
Duncan Sandsa8514532011-05-18 07:13:41 +00002926 if (SrcTy == DestTy)
2927 return BitCast;
2928
Matt Arsenaultcacbb232013-07-30 20:45:05 +00002929 // FIXME: Check address space sizes here
Chris Lattner229907c2011-07-18 04:54:35 +00002930 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
2931 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
Duncan Sandsa8514532011-05-18 07:13:41 +00002932 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2933 // An element by element cast. Find the appropriate opcode based on the
2934 // element types.
2935 SrcTy = SrcVecTy->getElementType();
2936 DestTy = DestVecTy->getElementType();
2937 }
2938
2939 // Get the bit sizes, we'll need these
Duncan Sands7f646562011-05-18 09:21:57 +00002940 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2941 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
Duncan Sandsa8514532011-05-18 07:13:41 +00002942
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002943 // Run through the possibilities ...
Duncan Sands9dff9be2010-02-15 16:12:20 +00002944 if (DestTy->isIntegerTy()) { // Casting to integral
2945 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002946 if (DestBits < SrcBits)
2947 return Trunc; // int -> smaller int
2948 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00002949 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002950 return SExt; // signed -> SEXT
2951 else
2952 return ZExt; // unsigned -> ZEXT
2953 } else {
2954 return BitCast; // Same size, No-op cast
2955 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002956 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00002957 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002958 return FPToSI; // FP -> sint
2959 else
2960 return FPToUI; // FP -> uint
Duncan Sands27bd0df2011-05-18 10:59:25 +00002961 } else if (SrcTy->isVectorTy()) {
2962 assert(DestBits == SrcBits &&
2963 "Casting vector to integer of different width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002964 return BitCast; // Same size, no-op cast
2965 } else {
Duncan Sands19d0b472010-02-16 11:11:14 +00002966 assert(SrcTy->isPointerTy() &&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002967 "Casting from a value that is not first-class type");
2968 return PtrToInt; // ptr -> int
2969 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002970 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2971 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00002972 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002973 return SIToFP; // sint -> FP
2974 else
2975 return UIToFP; // uint -> FP
Duncan Sands9dff9be2010-02-15 16:12:20 +00002976 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002977 if (DestBits < SrcBits) {
2978 return FPTrunc; // FP -> smaller FP
2979 } else if (DestBits > SrcBits) {
2980 return FPExt; // FP -> larger FP
2981 } else {
2982 return BitCast; // same size, no-op cast
2983 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00002984 } else if (SrcTy->isVectorTy()) {
2985 assert(DestBits == SrcBits &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002986 "Casting vector to floating point of different width");
Devang Patele9432132008-11-05 01:37:40 +00002987 return BitCast; // same size, no-op cast
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002988 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002989 llvm_unreachable("Casting pointer or non-first class to float");
Duncan Sands27bd0df2011-05-18 10:59:25 +00002990 } else if (DestTy->isVectorTy()) {
2991 assert(DestBits == SrcBits &&
2992 "Illegal cast to vector (wrong type or size)");
2993 return BitCast;
Duncan Sands19d0b472010-02-16 11:11:14 +00002994 } else if (DestTy->isPointerTy()) {
2995 if (SrcTy->isPointerTy()) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002996 if (DestTy->getPointerAddressSpace() != SrcTy->getPointerAddressSpace())
2997 return AddrSpaceCast;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002998 return BitCast; // ptr -> ptr
Duncan Sands9dff9be2010-02-15 16:12:20 +00002999 } else if (SrcTy->isIntegerTy()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003000 return IntToPtr; // int -> ptr
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003001 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00003002 llvm_unreachable("Casting pointer to other than pointer or int");
Dale Johannesendd224d22010-09-30 23:57:10 +00003003 } else if (DestTy->isX86_MMXTy()) {
Duncan Sands27bd0df2011-05-18 10:59:25 +00003004 if (SrcTy->isVectorTy()) {
3005 assert(DestBits == SrcBits && "Casting vector of wrong width to X86_MMX");
Dale Johannesendd224d22010-09-30 23:57:10 +00003006 return BitCast; // 64-bit vector to MMX
Dale Johannesendd224d22010-09-30 23:57:10 +00003007 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00003008 llvm_unreachable("Illegal cast to X86_MMX");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003009 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00003010 llvm_unreachable("Casting to type that is not first-class");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003011}
3012
3013//===----------------------------------------------------------------------===//
3014// CastInst SubClass Constructors
3015//===----------------------------------------------------------------------===//
3016
3017/// Check that the construction parameters for a CastInst are correct. This
3018/// could be broken out into the separate constructors but it is useful to have
3019/// it in one place and to eliminate the redundant code for getting the sizes
3020/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003021bool
Chris Lattner229907c2011-07-18 04:54:35 +00003022CastInst::castIsValid(Instruction::CastOps op, Value *S, Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003023 // Check for type sanity on the arguments
Chris Lattner229907c2011-07-18 04:54:35 +00003024 Type *SrcTy = S->getType();
Evan Cheng098d7b72013-01-10 23:22:53 +00003025
Chris Lattner37bc78a2010-01-26 21:51:43 +00003026 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType() ||
3027 SrcTy->isAggregateType() || DstTy->isAggregateType())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003028 return false;
3029
3030 // Get the size of the types in bits, we'll need this later
Dan Gohman7ccc52f2009-06-15 22:12:54 +00003031 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
3032 unsigned DstBitSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003033
Duncan Sands7f646562011-05-18 09:21:57 +00003034 // If these are vector types, get the lengths of the vectors (using zero for
3035 // scalar types means that checking that vector lengths match also checks that
3036 // scalars are not being converted to vectors or vectors to scalars).
3037 unsigned SrcLength = SrcTy->isVectorTy() ?
3038 cast<VectorType>(SrcTy)->getNumElements() : 0;
3039 unsigned DstLength = DstTy->isVectorTy() ?
3040 cast<VectorType>(DstTy)->getNumElements() : 0;
3041
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003042 // Switch on the opcode provided
3043 switch (op) {
3044 default: return false; // This is an input error
3045 case Instruction::Trunc:
Duncan Sands7f646562011-05-18 09:21:57 +00003046 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
3047 SrcLength == DstLength && SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003048 case Instruction::ZExt:
Duncan Sands7f646562011-05-18 09:21:57 +00003049 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
3050 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003051 case Instruction::SExt:
Duncan Sands7f646562011-05-18 09:21:57 +00003052 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
3053 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003054 case Instruction::FPTrunc:
Duncan Sands7f646562011-05-18 09:21:57 +00003055 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
3056 SrcLength == DstLength && SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003057 case Instruction::FPExt:
Duncan Sands7f646562011-05-18 09:21:57 +00003058 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
3059 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003060 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003061 case Instruction::SIToFP:
Duncan Sands7f646562011-05-18 09:21:57 +00003062 return SrcTy->isIntOrIntVectorTy() && DstTy->isFPOrFPVectorTy() &&
3063 SrcLength == DstLength;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003064 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003065 case Instruction::FPToSI:
Duncan Sands7f646562011-05-18 09:21:57 +00003066 return SrcTy->isFPOrFPVectorTy() && DstTy->isIntOrIntVectorTy() &&
3067 SrcLength == DstLength;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003068 case Instruction::PtrToInt:
Chris Lattner8a3df542012-01-25 01:32:59 +00003069 if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy))
Nadav Rotem3924cb02011-12-05 06:29:09 +00003070 return false;
Chris Lattner8a3df542012-01-25 01:32:59 +00003071 if (VectorType *VT = dyn_cast<VectorType>(SrcTy))
3072 if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements())
3073 return false;
Nadav Rotem3924cb02011-12-05 06:29:09 +00003074 return SrcTy->getScalarType()->isPointerTy() &&
3075 DstTy->getScalarType()->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003076 case Instruction::IntToPtr:
Chris Lattner8a3df542012-01-25 01:32:59 +00003077 if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy))
Nadav Rotem3924cb02011-12-05 06:29:09 +00003078 return false;
Chris Lattner8a3df542012-01-25 01:32:59 +00003079 if (VectorType *VT = dyn_cast<VectorType>(SrcTy))
3080 if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements())
3081 return false;
Nadav Rotem3924cb02011-12-05 06:29:09 +00003082 return SrcTy->getScalarType()->isIntegerTy() &&
3083 DstTy->getScalarType()->isPointerTy();
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003084 case Instruction::BitCast: {
3085 PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy->getScalarType());
3086 PointerType *DstPtrTy = dyn_cast<PointerType>(DstTy->getScalarType());
3087
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003088 // BitCast implies a no-op cast of type only. No bits change.
3089 // However, you can't cast pointers to anything but pointers.
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003090 if (!SrcPtrTy != !DstPtrTy)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003091 return false;
3092
Alp Tokerf907b892013-12-05 05:44:44 +00003093 // For non-pointer cases, the cast is okay if the source and destination bit
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003094 // widths are identical.
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003095 if (!SrcPtrTy)
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003096 return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits();
3097
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003098 // If both are pointers then the address spaces must match.
3099 if (SrcPtrTy->getAddressSpace() != DstPtrTy->getAddressSpace())
3100 return false;
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003101
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003102 // A vector of pointers must have the same number of elements.
3103 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) {
3104 if (VectorType *DstVecTy = dyn_cast<VectorType>(DstTy))
3105 return (SrcVecTy->getNumElements() == DstVecTy->getNumElements());
3106
3107 return false;
3108 }
3109
3110 return true;
3111 }
3112 case Instruction::AddrSpaceCast: {
3113 PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy->getScalarType());
3114 if (!SrcPtrTy)
3115 return false;
3116
3117 PointerType *DstPtrTy = dyn_cast<PointerType>(DstTy->getScalarType());
3118 if (!DstPtrTy)
3119 return false;
3120
3121 if (SrcPtrTy->getAddressSpace() == DstPtrTy->getAddressSpace())
3122 return false;
3123
3124 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) {
3125 if (VectorType *DstVecTy = dyn_cast<VectorType>(DstTy))
3126 return (SrcVecTy->getNumElements() == DstVecTy->getNumElements());
3127
3128 return false;
3129 }
3130
3131 return true;
3132 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003133 }
3134}
3135
3136TruncInst::TruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003137 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003138) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003139 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003140}
3141
3142TruncInst::TruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003143 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003144) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003145 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003146}
3147
3148ZExtInst::ZExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003149 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003150) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003151 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003152}
3153
3154ZExtInst::ZExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003155 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003156) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003157 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003158}
3159SExtInst::SExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003160 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003161) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003162 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003163}
3164
Jeff Cohencc08c832006-12-02 02:22:01 +00003165SExtInst::SExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003166 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003167) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003168 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003169}
3170
3171FPTruncInst::FPTruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003172 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003173) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003174 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003175}
3176
3177FPTruncInst::FPTruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003178 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003179) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003180 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003181}
3182
3183FPExtInst::FPExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003184 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003185) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003186 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003187}
3188
3189FPExtInst::FPExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003190 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003191) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003192 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003193}
3194
3195UIToFPInst::UIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003196 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003197) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003198 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003199}
3200
3201UIToFPInst::UIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003202 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003203) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003204 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003205}
3206
3207SIToFPInst::SIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003208 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003209) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003210 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003211}
3212
3213SIToFPInst::SIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003214 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003215) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003216 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003217}
3218
3219FPToUIInst::FPToUIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003220 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003221) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003222 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003223}
3224
3225FPToUIInst::FPToUIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003226 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003227) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003228 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003229}
3230
3231FPToSIInst::FPToSIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003232 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003233) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003234 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003235}
3236
3237FPToSIInst::FPToSIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003238 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003239) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003240 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003241}
3242
3243PtrToIntInst::PtrToIntInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003244 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003245) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003246 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003247}
3248
3249PtrToIntInst::PtrToIntInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003250 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003251) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003252 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003253}
3254
3255IntToPtrInst::IntToPtrInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003256 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003257) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003258 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003259}
3260
3261IntToPtrInst::IntToPtrInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003262 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003263) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003264 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003265}
3266
3267BitCastInst::BitCastInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003268 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003269) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003270 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003271}
3272
3273BitCastInst::BitCastInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003274 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003275) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003276 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003277}
Chris Lattnerf16dc002006-09-17 19:29:56 +00003278
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003279AddrSpaceCastInst::AddrSpaceCastInst(
3280 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
3281) : CastInst(Ty, AddrSpaceCast, S, Name, InsertBefore) {
3282 assert(castIsValid(getOpcode(), S, Ty) && "Illegal AddrSpaceCast");
3283}
3284
3285AddrSpaceCastInst::AddrSpaceCastInst(
3286 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
3287) : CastInst(Ty, AddrSpaceCast, S, Name, InsertAtEnd) {
3288 assert(castIsValid(getOpcode(), S, Ty) && "Illegal AddrSpaceCast");
3289}
3290
Chris Lattnerf16dc002006-09-17 19:29:56 +00003291//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00003292// CmpInst Classes
3293//===----------------------------------------------------------------------===//
3294
Craig Topper1c3f2832015-12-15 06:11:33 +00003295CmpInst::CmpInst(Type *ty, OtherOps op, Predicate predicate, Value *LHS,
3296 Value *RHS, const Twine &Name, Instruction *InsertBefore)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00003297 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00003298 OperandTraits<CmpInst>::op_begin(this),
3299 OperandTraits<CmpInst>::operands(this),
3300 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003301 Op<0>() = LHS;
3302 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00003303 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00003304 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003305}
Gabor Greiff6caff662008-05-10 08:32:32 +00003306
Craig Topper1c3f2832015-12-15 06:11:33 +00003307CmpInst::CmpInst(Type *ty, OtherOps op, Predicate predicate, Value *LHS,
3308 Value *RHS, const Twine &Name, BasicBlock *InsertAtEnd)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00003309 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00003310 OperandTraits<CmpInst>::op_begin(this),
3311 OperandTraits<CmpInst>::operands(this),
3312 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003313 Op<0>() = LHS;
3314 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00003315 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00003316 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003317}
3318
3319CmpInst *
Craig Topper1c3f2832015-12-15 06:11:33 +00003320CmpInst::Create(OtherOps Op, Predicate predicate, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00003321 const Twine &Name, Instruction *InsertBefore) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003322 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003323 if (InsertBefore)
3324 return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate),
3325 S1, S2, Name);
3326 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003327 return new ICmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003328 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003329 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003330
3331 if (InsertBefore)
3332 return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate),
3333 S1, S2, Name);
3334 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003335 return new FCmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003336 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003337}
3338
3339CmpInst *
Craig Topper1c3f2832015-12-15 06:11:33 +00003340CmpInst::Create(OtherOps Op, Predicate predicate, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00003341 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003342 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003343 return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
3344 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003345 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003346 return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
3347 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003348}
3349
3350void CmpInst::swapOperands() {
3351 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
3352 IC->swapOperands();
3353 else
3354 cast<FCmpInst>(this)->swapOperands();
3355}
3356
Duncan Sands95c4ecc2011-01-04 12:52:29 +00003357bool CmpInst::isCommutative() const {
3358 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00003359 return IC->isCommutative();
3360 return cast<FCmpInst>(this)->isCommutative();
3361}
3362
Duncan Sands95c4ecc2011-01-04 12:52:29 +00003363bool CmpInst::isEquality() const {
3364 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00003365 return IC->isEquality();
3366 return cast<FCmpInst>(this)->isEquality();
3367}
3368
Dan Gohman4e724382008-05-31 02:47:54 +00003369CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003370 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003371 default: llvm_unreachable("Unknown cmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00003372 case ICMP_EQ: return ICMP_NE;
3373 case ICMP_NE: return ICMP_EQ;
3374 case ICMP_UGT: return ICMP_ULE;
3375 case ICMP_ULT: return ICMP_UGE;
3376 case ICMP_UGE: return ICMP_ULT;
3377 case ICMP_ULE: return ICMP_UGT;
3378 case ICMP_SGT: return ICMP_SLE;
3379 case ICMP_SLT: return ICMP_SGE;
3380 case ICMP_SGE: return ICMP_SLT;
3381 case ICMP_SLE: return ICMP_SGT;
Reid Spencerd9436b62006-11-20 01:22:35 +00003382
Dan Gohman4e724382008-05-31 02:47:54 +00003383 case FCMP_OEQ: return FCMP_UNE;
3384 case FCMP_ONE: return FCMP_UEQ;
3385 case FCMP_OGT: return FCMP_ULE;
3386 case FCMP_OLT: return FCMP_UGE;
3387 case FCMP_OGE: return FCMP_ULT;
3388 case FCMP_OLE: return FCMP_UGT;
3389 case FCMP_UEQ: return FCMP_ONE;
3390 case FCMP_UNE: return FCMP_OEQ;
3391 case FCMP_UGT: return FCMP_OLE;
3392 case FCMP_ULT: return FCMP_OGE;
3393 case FCMP_UGE: return FCMP_OLT;
3394 case FCMP_ULE: return FCMP_OGT;
3395 case FCMP_ORD: return FCMP_UNO;
3396 case FCMP_UNO: return FCMP_ORD;
3397 case FCMP_TRUE: return FCMP_FALSE;
3398 case FCMP_FALSE: return FCMP_TRUE;
Reid Spencerd9436b62006-11-20 01:22:35 +00003399 }
3400}
3401
Tim Northoverde3aea0412016-08-17 20:25:25 +00003402StringRef CmpInst::getPredicateName(Predicate Pred) {
3403 switch (Pred) {
3404 default: return "unknown";
3405 case FCmpInst::FCMP_FALSE: return "false";
3406 case FCmpInst::FCMP_OEQ: return "oeq";
3407 case FCmpInst::FCMP_OGT: return "ogt";
3408 case FCmpInst::FCMP_OGE: return "oge";
3409 case FCmpInst::FCMP_OLT: return "olt";
3410 case FCmpInst::FCMP_OLE: return "ole";
3411 case FCmpInst::FCMP_ONE: return "one";
3412 case FCmpInst::FCMP_ORD: return "ord";
3413 case FCmpInst::FCMP_UNO: return "uno";
3414 case FCmpInst::FCMP_UEQ: return "ueq";
3415 case FCmpInst::FCMP_UGT: return "ugt";
3416 case FCmpInst::FCMP_UGE: return "uge";
3417 case FCmpInst::FCMP_ULT: return "ult";
3418 case FCmpInst::FCMP_ULE: return "ule";
3419 case FCmpInst::FCMP_UNE: return "une";
3420 case FCmpInst::FCMP_TRUE: return "true";
3421 case ICmpInst::ICMP_EQ: return "eq";
3422 case ICmpInst::ICMP_NE: return "ne";
3423 case ICmpInst::ICMP_SGT: return "sgt";
3424 case ICmpInst::ICMP_SGE: return "sge";
3425 case ICmpInst::ICMP_SLT: return "slt";
3426 case ICmpInst::ICMP_SLE: return "sle";
3427 case ICmpInst::ICMP_UGT: return "ugt";
3428 case ICmpInst::ICMP_UGE: return "uge";
3429 case ICmpInst::ICMP_ULT: return "ult";
3430 case ICmpInst::ICMP_ULE: return "ule";
3431 }
3432}
3433
Reid Spencer266e42b2006-12-23 06:05:41 +00003434ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
3435 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003436 default: llvm_unreachable("Unknown icmp predicate!");
Reid Spencer266e42b2006-12-23 06:05:41 +00003437 case ICMP_EQ: case ICMP_NE:
3438 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
3439 return pred;
3440 case ICMP_UGT: return ICMP_SGT;
3441 case ICMP_ULT: return ICMP_SLT;
3442 case ICMP_UGE: return ICMP_SGE;
3443 case ICMP_ULE: return ICMP_SLE;
3444 }
3445}
3446
Nick Lewycky8ea81e82008-01-28 03:48:02 +00003447ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
3448 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003449 default: llvm_unreachable("Unknown icmp predicate!");
Nick Lewycky8ea81e82008-01-28 03:48:02 +00003450 case ICMP_EQ: case ICMP_NE:
3451 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
3452 return pred;
3453 case ICMP_SGT: return ICMP_UGT;
3454 case ICMP_SLT: return ICMP_ULT;
3455 case ICMP_SGE: return ICMP_UGE;
3456 case ICMP_SLE: return ICMP_ULE;
3457 }
3458}
3459
Dan Gohman4e724382008-05-31 02:47:54 +00003460CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003461 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003462 default: llvm_unreachable("Unknown cmp predicate!");
Dan Gohman4e724382008-05-31 02:47:54 +00003463 case ICMP_EQ: case ICMP_NE:
3464 return pred;
3465 case ICMP_SGT: return ICMP_SLT;
3466 case ICMP_SLT: return ICMP_SGT;
3467 case ICMP_SGE: return ICMP_SLE;
3468 case ICMP_SLE: return ICMP_SGE;
3469 case ICMP_UGT: return ICMP_ULT;
3470 case ICMP_ULT: return ICMP_UGT;
3471 case ICMP_UGE: return ICMP_ULE;
3472 case ICMP_ULE: return ICMP_UGE;
3473
Reid Spencerd9436b62006-11-20 01:22:35 +00003474 case FCMP_FALSE: case FCMP_TRUE:
3475 case FCMP_OEQ: case FCMP_ONE:
3476 case FCMP_UEQ: case FCMP_UNE:
3477 case FCMP_ORD: case FCMP_UNO:
3478 return pred;
3479 case FCMP_OGT: return FCMP_OLT;
3480 case FCMP_OLT: return FCMP_OGT;
3481 case FCMP_OGE: return FCMP_OLE;
3482 case FCMP_OLE: return FCMP_OGE;
3483 case FCMP_UGT: return FCMP_ULT;
3484 case FCMP_ULT: return FCMP_UGT;
3485 case FCMP_UGE: return FCMP_ULE;
3486 case FCMP_ULE: return FCMP_UGE;
3487 }
3488}
3489
Sanjoy Das6e78b172015-10-22 19:57:34 +00003490CmpInst::Predicate CmpInst::getSignedPredicate(Predicate pred) {
3491 assert(CmpInst::isUnsigned(pred) && "Call only with signed predicates!");
3492
3493 switch (pred) {
3494 default:
3495 llvm_unreachable("Unknown predicate!");
3496 case CmpInst::ICMP_ULT:
3497 return CmpInst::ICMP_SLT;
3498 case CmpInst::ICMP_ULE:
3499 return CmpInst::ICMP_SLE;
3500 case CmpInst::ICMP_UGT:
3501 return CmpInst::ICMP_SGT;
3502 case CmpInst::ICMP_UGE:
3503 return CmpInst::ICMP_SGE;
3504 }
3505}
3506
Craig Topper1c3f2832015-12-15 06:11:33 +00003507bool CmpInst::isUnsigned(Predicate predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003508 switch (predicate) {
3509 default: return false;
3510 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
3511 case ICmpInst::ICMP_UGE: return true;
3512 }
3513}
3514
Craig Topper1c3f2832015-12-15 06:11:33 +00003515bool CmpInst::isSigned(Predicate predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003516 switch (predicate) {
3517 default: return false;
3518 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
3519 case ICmpInst::ICMP_SGE: return true;
3520 }
3521}
3522
Craig Topper1c3f2832015-12-15 06:11:33 +00003523bool CmpInst::isOrdered(Predicate predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003524 switch (predicate) {
3525 default: return false;
3526 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
3527 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
3528 case FCmpInst::FCMP_ORD: return true;
3529 }
3530}
3531
Craig Topper1c3f2832015-12-15 06:11:33 +00003532bool CmpInst::isUnordered(Predicate predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003533 switch (predicate) {
3534 default: return false;
3535 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
3536 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
3537 case FCmpInst::FCMP_UNO: return true;
3538 }
3539}
3540
Craig Topper1c3f2832015-12-15 06:11:33 +00003541bool CmpInst::isTrueWhenEqual(Predicate predicate) {
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003542 switch(predicate) {
3543 default: return false;
3544 case ICMP_EQ: case ICMP_UGE: case ICMP_ULE: case ICMP_SGE: case ICMP_SLE:
3545 case FCMP_TRUE: case FCMP_UEQ: case FCMP_UGE: case FCMP_ULE: return true;
3546 }
3547}
3548
Craig Topper1c3f2832015-12-15 06:11:33 +00003549bool CmpInst::isFalseWhenEqual(Predicate predicate) {
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003550 switch(predicate) {
3551 case ICMP_NE: case ICMP_UGT: case ICMP_ULT: case ICMP_SGT: case ICMP_SLT:
3552 case FCMP_FALSE: case FCMP_ONE: case FCMP_OGT: case FCMP_OLT: return true;
3553 default: return false;
3554 }
3555}
3556
Chad Rosier99bc4802016-04-21 16:18:02 +00003557bool CmpInst::isImpliedTrueByMatchingCmp(Predicate Pred1, Predicate Pred2) {
Chad Rosieraf83e402016-04-21 14:04:54 +00003558 // If the predicates match, then we know the first condition implies the
3559 // second is true.
3560 if (Pred1 == Pred2)
3561 return true;
3562
3563 switch (Pred1) {
3564 default:
3565 break;
Chad Rosier1a601592016-04-22 17:57:34 +00003566 case ICMP_EQ:
Chad Rosier3d75f8c2016-04-25 13:25:14 +00003567 // 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 +00003568 return Pred2 == ICMP_UGE || Pred2 == ICMP_ULE || Pred2 == ICMP_SGE ||
3569 Pred2 == ICMP_SLE;
Chad Rosier3456cb52016-04-22 17:14:12 +00003570 case ICMP_UGT: // A >u B implies A != B and A >=u B are true.
3571 return Pred2 == ICMP_NE || Pred2 == ICMP_UGE;
3572 case ICMP_ULT: // A <u B implies A != B and A <=u B are true.
3573 return Pred2 == ICMP_NE || Pred2 == ICMP_ULE;
3574 case ICMP_SGT: // A >s B implies A != B and A >=s B are true.
3575 return Pred2 == ICMP_NE || Pred2 == ICMP_SGE;
3576 case ICMP_SLT: // A <s B implies A != B and A <=s B are true.
3577 return Pred2 == ICMP_NE || Pred2 == ICMP_SLE;
Chad Rosieraf83e402016-04-21 14:04:54 +00003578 }
3579 return false;
3580}
3581
Chad Rosier99bc4802016-04-21 16:18:02 +00003582bool CmpInst::isImpliedFalseByMatchingCmp(Predicate Pred1, Predicate Pred2) {
Chad Rosier1a601592016-04-22 17:57:34 +00003583 return isImpliedTrueByMatchingCmp(Pred1, getInversePredicate(Pred2));
Chad Rosieraf83e402016-04-21 14:04:54 +00003584}
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003585
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003586//===----------------------------------------------------------------------===//
3587// SwitchInst Implementation
3588//===----------------------------------------------------------------------===//
3589
Chris Lattnerbaf00152010-11-17 05:41:46 +00003590void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumReserved) {
3591 assert(Value && Default && NumReserved);
3592 ReservedSpace = NumReserved;
Pete Cooperb4eede22015-06-12 17:48:10 +00003593 setNumHungOffUseOperands(2);
Pete Cooper3fc30402015-06-10 22:38:46 +00003594 allocHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003595
Pete Coopereb31b682015-05-21 22:48:54 +00003596 Op<0>() = Value;
3597 Op<1>() = Default;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003598}
3599
Chris Lattner2195fc42007-02-24 00:55:48 +00003600/// SwitchInst ctor - Create a new switch instruction, specifying a value to
3601/// switch on and a default destination. The number of additional cases can
3602/// be specified here to make memory allocation more efficient. This
3603/// constructor can also autoinsert before another instruction.
3604SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3605 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00003606 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
Craig Topperc6207612014-04-09 06:08:46 +00003607 nullptr, 0, InsertBefore) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003608 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00003609}
3610
3611/// SwitchInst ctor - Create a new switch instruction, specifying a value to
3612/// switch on and a default destination. The number of additional cases can
3613/// be specified here to make memory allocation more efficient. This
3614/// constructor also autoinserts at the end of the specified BasicBlock.
3615SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3616 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00003617 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
Craig Topperc6207612014-04-09 06:08:46 +00003618 nullptr, 0, InsertAtEnd) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003619 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00003620}
3621
Misha Brukmanb1c93172005-04-21 23:48:37 +00003622SwitchInst::SwitchInst(const SwitchInst &SI)
Craig Topperc6207612014-04-09 06:08:46 +00003623 : TerminatorInst(SI.getType(), Instruction::Switch, nullptr, 0) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003624 init(SI.getCondition(), SI.getDefaultDest(), SI.getNumOperands());
Pete Cooperb4eede22015-06-12 17:48:10 +00003625 setNumHungOffUseOperands(SI.getNumOperands());
Pete Cooper74510a42015-06-12 17:48:05 +00003626 Use *OL = getOperandList();
3627 const Use *InOL = SI.getOperandList();
Chris Lattnerbaf00152010-11-17 05:41:46 +00003628 for (unsigned i = 2, E = SI.getNumOperands(); i != E; i += 2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003629 OL[i] = InOL[i];
3630 OL[i+1] = InOL[i+1];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003631 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003632 SubclassOptionalData = SI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003633}
3634
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003635
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003636/// addCase - Add an entry to the switch instruction...
3637///
Chris Lattner47ac1872005-02-24 05:32:09 +00003638void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Pete Cooperb4eede22015-06-12 17:48:10 +00003639 unsigned NewCaseIdx = getNumCases();
3640 unsigned OpNo = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003641 if (OpNo+2 > ReservedSpace)
Jay Foade98f29d2011-04-01 08:00:58 +00003642 growOperands(); // Get more space!
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003643 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00003644 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Pete Cooperb4eede22015-06-12 17:48:10 +00003645 setNumHungOffUseOperands(OpNo+2);
Chandler Carruth927d8e62017-04-12 07:27:28 +00003646 CaseHandle Case(this, NewCaseIdx);
Bob Wilsone4077362013-09-09 19:14:35 +00003647 Case.setValue(OnVal);
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00003648 Case.setSuccessor(Dest);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003649}
3650
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003651/// removeCase - This method removes the specified case and its successor
3652/// from the switch instruction.
Chandler Carruth927d8e62017-04-12 07:27:28 +00003653SwitchInst::CaseIt SwitchInst::removeCase(CaseIt I) {
3654 unsigned idx = I->getCaseIndex();
3655
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003656 assert(2 + idx*2 < getNumOperands() && "Case index out of range!!!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003657
3658 unsigned NumOps = getNumOperands();
Pete Cooper74510a42015-06-12 17:48:05 +00003659 Use *OL = getOperandList();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003660
Jay Foad14277722011-02-01 09:22:34 +00003661 // Overwrite this case with the end of the list.
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003662 if (2 + (idx + 1) * 2 != NumOps) {
3663 OL[2 + idx * 2] = OL[NumOps - 2];
3664 OL[2 + idx * 2 + 1] = OL[NumOps - 1];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003665 }
3666
3667 // Nuke the last value.
Craig Topperc6207612014-04-09 06:08:46 +00003668 OL[NumOps-2].set(nullptr);
3669 OL[NumOps-2+1].set(nullptr);
Pete Cooperb4eede22015-06-12 17:48:10 +00003670 setNumHungOffUseOperands(NumOps-2);
Chandler Carruth0d256c02017-03-26 02:49:23 +00003671
3672 return CaseIt(this, idx);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003673}
3674
Jay Foade98f29d2011-04-01 08:00:58 +00003675/// growOperands - grow operands - This grows the operand list in response
3676/// to a push_back style of operation. This grows the number of ops by 3 times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003677///
Jay Foade98f29d2011-04-01 08:00:58 +00003678void SwitchInst::growOperands() {
Gabor Greiff6caff662008-05-10 08:32:32 +00003679 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +00003680 unsigned NumOps = e*3;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003681
3682 ReservedSpace = NumOps;
Pete Cooper93f9ff52015-06-10 22:38:41 +00003683 growHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003684}
3685
Chris Lattner3ed871f2009-10-27 19:13:16 +00003686//===----------------------------------------------------------------------===//
Jay Foadbbb91f22011-01-16 15:30:52 +00003687// IndirectBrInst Implementation
Chris Lattner3ed871f2009-10-27 19:13:16 +00003688//===----------------------------------------------------------------------===//
3689
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003690void IndirectBrInst::init(Value *Address, unsigned NumDests) {
Duncan Sands19d0b472010-02-16 11:11:14 +00003691 assert(Address && Address->getType()->isPointerTy() &&
Chris Lattner6747b4c2009-10-29 05:53:32 +00003692 "Address of indirectbr must be a pointer");
Chris Lattner3ed871f2009-10-27 19:13:16 +00003693 ReservedSpace = 1+NumDests;
Pete Cooperb4eede22015-06-12 17:48:10 +00003694 setNumHungOffUseOperands(1);
Pete Cooper3fc30402015-06-10 22:38:46 +00003695 allocHungoffUses(ReservedSpace);
3696
Pete Coopereb31b682015-05-21 22:48:54 +00003697 Op<0>() = Address;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003698}
3699
3700
Jay Foade98f29d2011-04-01 08:00:58 +00003701/// growOperands - grow operands - This grows the operand list in response
3702/// to a push_back style of operation. This grows the number of ops by 2 times.
Chris Lattner3ed871f2009-10-27 19:13:16 +00003703///
Jay Foade98f29d2011-04-01 08:00:58 +00003704void IndirectBrInst::growOperands() {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003705 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +00003706 unsigned NumOps = e*2;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003707
3708 ReservedSpace = NumOps;
Pete Cooper93f9ff52015-06-10 22:38:41 +00003709 growHungoffUses(ReservedSpace);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003710}
3711
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003712IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3713 Instruction *InsertBefore)
3714: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Craig Topperc6207612014-04-09 06:08:46 +00003715 nullptr, 0, InsertBefore) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003716 init(Address, NumCases);
3717}
3718
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003719IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3720 BasicBlock *InsertAtEnd)
3721: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Craig Topperc6207612014-04-09 06:08:46 +00003722 nullptr, 0, InsertAtEnd) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003723 init(Address, NumCases);
3724}
3725
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003726IndirectBrInst::IndirectBrInst(const IndirectBrInst &IBI)
Pete Cooper3fc30402015-06-10 22:38:46 +00003727 : TerminatorInst(Type::getVoidTy(IBI.getContext()), Instruction::IndirectBr,
3728 nullptr, IBI.getNumOperands()) {
3729 allocHungoffUses(IBI.getNumOperands());
Pete Cooper74510a42015-06-12 17:48:05 +00003730 Use *OL = getOperandList();
3731 const Use *InOL = IBI.getOperandList();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003732 for (unsigned i = 0, E = IBI.getNumOperands(); i != E; ++i)
3733 OL[i] = InOL[i];
3734 SubclassOptionalData = IBI.SubclassOptionalData;
3735}
3736
Chris Lattner3ed871f2009-10-27 19:13:16 +00003737/// addDestination - Add a destination.
3738///
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003739void IndirectBrInst::addDestination(BasicBlock *DestBB) {
Pete Cooperb4eede22015-06-12 17:48:10 +00003740 unsigned OpNo = getNumOperands();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003741 if (OpNo+1 > ReservedSpace)
Jay Foade98f29d2011-04-01 08:00:58 +00003742 growOperands(); // Get more space!
Chris Lattner3ed871f2009-10-27 19:13:16 +00003743 // Initialize some new operands.
3744 assert(OpNo < ReservedSpace && "Growing didn't work!");
Pete Cooperb4eede22015-06-12 17:48:10 +00003745 setNumHungOffUseOperands(OpNo+1);
Pete Cooper74510a42015-06-12 17:48:05 +00003746 getOperandList()[OpNo] = DestBB;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003747}
3748
3749/// removeDestination - This method removes the specified successor from the
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003750/// indirectbr instruction.
3751void IndirectBrInst::removeDestination(unsigned idx) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003752 assert(idx < getNumOperands()-1 && "Successor index out of range!");
3753
3754 unsigned NumOps = getNumOperands();
Pete Cooper74510a42015-06-12 17:48:05 +00003755 Use *OL = getOperandList();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003756
3757 // Replace this value with the last one.
3758 OL[idx+1] = OL[NumOps-1];
3759
3760 // Nuke the last value.
Craig Topperc6207612014-04-09 06:08:46 +00003761 OL[NumOps-1].set(nullptr);
Pete Cooperb4eede22015-06-12 17:48:10 +00003762 setNumHungOffUseOperands(NumOps-1);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003763}
3764
Chris Lattner3ed871f2009-10-27 19:13:16 +00003765//===----------------------------------------------------------------------===//
Pete Cooper75403d72015-06-24 20:22:23 +00003766// cloneImpl() implementations
Chris Lattner3ed871f2009-10-27 19:13:16 +00003767//===----------------------------------------------------------------------===//
3768
Chris Lattnerf22be932004-10-15 23:52:53 +00003769// Define these methods here so vtables don't get emitted into every translation
3770// unit that uses these classes.
3771
Pete Cooper75403d72015-06-24 20:22:23 +00003772GetElementPtrInst *GetElementPtrInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003773 return new (getNumOperands()) GetElementPtrInst(*this);
Chris Lattnerf22be932004-10-15 23:52:53 +00003774}
3775
Pete Cooper75403d72015-06-24 20:22:23 +00003776BinaryOperator *BinaryOperator::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003777 return Create(getOpcode(), Op<0>(), Op<1>());
Chris Lattnerf22be932004-10-15 23:52:53 +00003778}
3779
Pete Cooper75403d72015-06-24 20:22:23 +00003780FCmpInst *FCmpInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003781 return new FCmpInst(getPredicate(), Op<0>(), Op<1>());
Reid Spencerd9436b62006-11-20 01:22:35 +00003782}
3783
Pete Cooper75403d72015-06-24 20:22:23 +00003784ICmpInst *ICmpInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003785 return new ICmpInst(getPredicate(), Op<0>(), Op<1>());
Dan Gohman0752bff2008-05-23 00:36:11 +00003786}
3787
Pete Cooper75403d72015-06-24 20:22:23 +00003788ExtractValueInst *ExtractValueInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003789 return new ExtractValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003790}
3791
Pete Cooper75403d72015-06-24 20:22:23 +00003792InsertValueInst *InsertValueInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003793 return new InsertValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003794}
3795
Pete Cooper75403d72015-06-24 20:22:23 +00003796AllocaInst *AllocaInst::cloneImpl() const {
David Majnemer6b3244c2014-04-30 16:12:21 +00003797 AllocaInst *Result = new AllocaInst(getAllocatedType(),
Matt Arsenault3c1fc762017-04-10 22:27:50 +00003798 getType()->getAddressSpace(),
David Majnemer6b3244c2014-04-30 16:12:21 +00003799 (Value *)getOperand(0), getAlignment());
3800 Result->setUsedWithInAlloca(isUsedWithInAlloca());
Manman Ren9bfd0d02016-04-01 21:41:15 +00003801 Result->setSwiftError(isSwiftError());
David Majnemer6b3244c2014-04-30 16:12:21 +00003802 return Result;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003803}
3804
Pete Cooper75403d72015-06-24 20:22:23 +00003805LoadInst *LoadInst::cloneImpl() const {
Eli Friedman59b66882011-08-09 23:02:53 +00003806 return new LoadInst(getOperand(0), Twine(), isVolatile(),
3807 getAlignment(), getOrdering(), getSynchScope());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003808}
3809
Pete Cooper75403d72015-06-24 20:22:23 +00003810StoreInst *StoreInst::cloneImpl() const {
Eli Friedmancad9f2a2011-08-10 17:39:11 +00003811 return new StoreInst(getOperand(0), getOperand(1), isVolatile(),
Eli Friedman59b66882011-08-09 23:02:53 +00003812 getAlignment(), getOrdering(), getSynchScope());
3813
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003814}
3815
Pete Cooper75403d72015-06-24 20:22:23 +00003816AtomicCmpXchgInst *AtomicCmpXchgInst::cloneImpl() const {
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003817 AtomicCmpXchgInst *Result =
3818 new AtomicCmpXchgInst(getOperand(0), getOperand(1), getOperand(2),
Tim Northovere94a5182014-03-11 10:48:52 +00003819 getSuccessOrdering(), getFailureOrdering(),
3820 getSynchScope());
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003821 Result->setVolatile(isVolatile());
Tim Northover420a2162014-06-13 14:24:07 +00003822 Result->setWeak(isWeak());
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003823 return Result;
3824}
3825
Pete Cooper75403d72015-06-24 20:22:23 +00003826AtomicRMWInst *AtomicRMWInst::cloneImpl() const {
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003827 AtomicRMWInst *Result =
3828 new AtomicRMWInst(getOperation(),getOperand(0), getOperand(1),
3829 getOrdering(), getSynchScope());
3830 Result->setVolatile(isVolatile());
3831 return Result;
3832}
3833
Pete Cooper75403d72015-06-24 20:22:23 +00003834FenceInst *FenceInst::cloneImpl() const {
Eli Friedmanfee02c62011-07-25 23:16:38 +00003835 return new FenceInst(getContext(), getOrdering(), getSynchScope());
3836}
3837
Pete Cooper75403d72015-06-24 20:22:23 +00003838TruncInst *TruncInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003839 return new TruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003840}
3841
Pete Cooper75403d72015-06-24 20:22:23 +00003842ZExtInst *ZExtInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003843 return new ZExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003844}
3845
Pete Cooper75403d72015-06-24 20:22:23 +00003846SExtInst *SExtInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003847 return new SExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003848}
3849
Pete Cooper75403d72015-06-24 20:22:23 +00003850FPTruncInst *FPTruncInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003851 return new FPTruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003852}
3853
Pete Cooper75403d72015-06-24 20:22:23 +00003854FPExtInst *FPExtInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003855 return new FPExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003856}
3857
Pete Cooper75403d72015-06-24 20:22:23 +00003858UIToFPInst *UIToFPInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003859 return new UIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003860}
3861
Pete Cooper75403d72015-06-24 20:22:23 +00003862SIToFPInst *SIToFPInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003863 return new SIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003864}
3865
Pete Cooper75403d72015-06-24 20:22:23 +00003866FPToUIInst *FPToUIInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003867 return new FPToUIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003868}
3869
Pete Cooper75403d72015-06-24 20:22:23 +00003870FPToSIInst *FPToSIInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003871 return new FPToSIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003872}
3873
Pete Cooper75403d72015-06-24 20:22:23 +00003874PtrToIntInst *PtrToIntInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003875 return new PtrToIntInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003876}
3877
Pete Cooper75403d72015-06-24 20:22:23 +00003878IntToPtrInst *IntToPtrInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003879 return new IntToPtrInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003880}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003881
Pete Cooper75403d72015-06-24 20:22:23 +00003882BitCastInst *BitCastInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003883 return new BitCastInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003884}
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003885
Pete Cooper75403d72015-06-24 20:22:23 +00003886AddrSpaceCastInst *AddrSpaceCastInst::cloneImpl() const {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003887 return new AddrSpaceCastInst(getOperand(0), getType());
3888}
3889
Pete Cooper75403d72015-06-24 20:22:23 +00003890CallInst *CallInst::cloneImpl() const {
Sanjoy Dasbd1c1bf2015-11-10 20:13:21 +00003891 if (hasOperandBundles()) {
3892 unsigned DescriptorBytes = getNumOperandBundles() * sizeof(BundleOpInfo);
3893 return new(getNumOperands(), DescriptorBytes) CallInst(*this);
3894 }
Devang Patel11cf3f42009-10-27 22:16:29 +00003895 return new(getNumOperands()) CallInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003896}
3897
Pete Cooper75403d72015-06-24 20:22:23 +00003898SelectInst *SelectInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003899 return SelectInst::Create(getOperand(0), getOperand(1), getOperand(2));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003900}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003901
Pete Cooper75403d72015-06-24 20:22:23 +00003902VAArgInst *VAArgInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003903 return new VAArgInst(getOperand(0), getType());
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003904}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003905
Pete Cooper75403d72015-06-24 20:22:23 +00003906ExtractElementInst *ExtractElementInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003907 return ExtractElementInst::Create(getOperand(0), getOperand(1));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003908}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003909
Pete Cooper75403d72015-06-24 20:22:23 +00003910InsertElementInst *InsertElementInst::cloneImpl() const {
Chris Lattner1dcb6542012-01-25 23:49:49 +00003911 return InsertElementInst::Create(getOperand(0), getOperand(1), getOperand(2));
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003912}
3913
Pete Cooper75403d72015-06-24 20:22:23 +00003914ShuffleVectorInst *ShuffleVectorInst::cloneImpl() const {
Chris Lattner1dcb6542012-01-25 23:49:49 +00003915 return new ShuffleVectorInst(getOperand(0), getOperand(1), getOperand(2));
Gabor Greif697e94c2008-05-15 10:04:30 +00003916}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003917
Pete Cooper75403d72015-06-24 20:22:23 +00003918PHINode *PHINode::cloneImpl() const { return new PHINode(*this); }
Devang Patel11cf3f42009-10-27 22:16:29 +00003919
Pete Cooper75403d72015-06-24 20:22:23 +00003920LandingPadInst *LandingPadInst::cloneImpl() const {
Bill Wendlingfae14752011-08-12 20:24:12 +00003921 return new LandingPadInst(*this);
3922}
3923
Pete Cooper75403d72015-06-24 20:22:23 +00003924ReturnInst *ReturnInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003925 return new(getNumOperands()) ReturnInst(*this);
3926}
3927
Pete Cooper75403d72015-06-24 20:22:23 +00003928BranchInst *BranchInst::cloneImpl() const {
Jay Foadd81f3c92011-01-07 20:29:02 +00003929 return new(getNumOperands()) BranchInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003930}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003931
Pete Cooper75403d72015-06-24 20:22:23 +00003932SwitchInst *SwitchInst::cloneImpl() const { return new SwitchInst(*this); }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003933
Pete Cooper75403d72015-06-24 20:22:23 +00003934IndirectBrInst *IndirectBrInst::cloneImpl() const {
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003935 return new IndirectBrInst(*this);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003936}
3937
Pete Cooper75403d72015-06-24 20:22:23 +00003938InvokeInst *InvokeInst::cloneImpl() const {
Sanjoy Dasbd1c1bf2015-11-10 20:13:21 +00003939 if (hasOperandBundles()) {
3940 unsigned DescriptorBytes = getNumOperandBundles() * sizeof(BundleOpInfo);
3941 return new(getNumOperands(), DescriptorBytes) InvokeInst(*this);
3942 }
Devang Patel11cf3f42009-10-27 22:16:29 +00003943 return new(getNumOperands()) InvokeInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003944}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003945
Pete Cooper75403d72015-06-24 20:22:23 +00003946ResumeInst *ResumeInst::cloneImpl() const { return new (1) ResumeInst(*this); }
Bill Wendlingf891bf82011-07-31 06:30:59 +00003947
David Majnemer654e1302015-07-31 17:58:14 +00003948CleanupReturnInst *CleanupReturnInst::cloneImpl() const {
3949 return new (getNumOperands()) CleanupReturnInst(*this);
3950}
3951
David Majnemer654e1302015-07-31 17:58:14 +00003952CatchReturnInst *CatchReturnInst::cloneImpl() const {
David Majnemer0bc0eef2015-08-15 02:46:08 +00003953 return new (getNumOperands()) CatchReturnInst(*this);
David Majnemer654e1302015-07-31 17:58:14 +00003954}
3955
David Majnemer8a1c45d2015-12-12 05:38:55 +00003956CatchSwitchInst *CatchSwitchInst::cloneImpl() const {
3957 return new CatchSwitchInst(*this);
3958}
3959
3960FuncletPadInst *FuncletPadInst::cloneImpl() const {
3961 return new (getNumOperands()) FuncletPadInst(*this);
David Majnemer654e1302015-07-31 17:58:14 +00003962}
3963
Pete Cooper75403d72015-06-24 20:22:23 +00003964UnreachableInst *UnreachableInst::cloneImpl() const {
Nick Lewycky42fb7452009-09-27 07:38:41 +00003965 LLVMContext &Context = getContext();
Devang Patel11cf3f42009-10-27 22:16:29 +00003966 return new UnreachableInst(Context);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003967}