blob: 4ee91aaf99a155f10316bb94412bc48e33bdc187 [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//===----------------------------------------------------------------------===//
Bjorn Pettersson550517b2018-06-26 06:17:00 +000048// AllocaInst Class
49//===----------------------------------------------------------------------===//
50
51Optional<uint64_t>
52AllocaInst::getAllocationSizeInBits(const DataLayout &DL) const {
53 uint64_t Size = DL.getTypeAllocSizeInBits(getAllocatedType());
54 if (isArrayAllocation()) {
55 auto C = dyn_cast<ConstantInt>(getArraySize());
56 if (!C)
57 return None;
58 Size *= C->getZExtValue();
59 }
60 return Size;
61}
62
63//===----------------------------------------------------------------------===//
Chris Lattner3e13b8c2008-01-02 23:42:30 +000064// CallSite Class
65//===----------------------------------------------------------------------===//
66
Gabor Greifa2fbc0a2010-03-24 13:21:49 +000067User::op_iterator CallSite::getCallee() const {
68 Instruction *II(getInstruction());
69 return isCall()
Gabor Greif638c8232010-08-05 21:25:49 +000070 ? cast<CallInst>(II)->op_end() - 1 // Skip Callee
Gabor Greif6d673952010-07-16 09:38:02 +000071 : cast<InvokeInst>(II)->op_end() - 3; // Skip BB, BB, Callee
Gabor Greifa2fbc0a2010-03-24 13:21:49 +000072}
73
Gordon Henriksen14a55692007-12-10 02:14:30 +000074//===----------------------------------------------------------------------===//
Chris Lattner88107952008-12-29 00:12:50 +000075// SelectInst Class
76//===----------------------------------------------------------------------===//
77
78/// areInvalidOperands - Return a string if the specified operands are invalid
79/// for a select operation, otherwise return null.
80const char *SelectInst::areInvalidOperands(Value *Op0, Value *Op1, Value *Op2) {
81 if (Op1->getType() != Op2->getType())
82 return "both values to select must have same type";
David Majnemerb611e3f2015-08-14 05:09:07 +000083
84 if (Op1->getType()->isTokenTy())
85 return "select values cannot have token type";
86
Chris Lattner229907c2011-07-18 04:54:35 +000087 if (VectorType *VT = dyn_cast<VectorType>(Op0->getType())) {
Chris Lattner88107952008-12-29 00:12:50 +000088 // Vector select.
Owen Anderson55f1c092009-08-13 21:58:54 +000089 if (VT->getElementType() != Type::getInt1Ty(Op0->getContext()))
Chris Lattner88107952008-12-29 00:12:50 +000090 return "vector select condition element type must be i1";
Chris Lattner229907c2011-07-18 04:54:35 +000091 VectorType *ET = dyn_cast<VectorType>(Op1->getType());
Craig Topperc6207612014-04-09 06:08:46 +000092 if (!ET)
Chris Lattner88107952008-12-29 00:12:50 +000093 return "selected values for vector select must be vectors";
94 if (ET->getNumElements() != VT->getNumElements())
95 return "vector select requires selected vectors to have "
96 "the same vector length as select condition";
Owen Anderson55f1c092009-08-13 21:58:54 +000097 } else if (Op0->getType() != Type::getInt1Ty(Op0->getContext())) {
Chris Lattner88107952008-12-29 00:12:50 +000098 return "select condition must be i1 or <n x i1>";
99 }
Craig Topperc6207612014-04-09 06:08:46 +0000100 return nullptr;
Chris Lattner88107952008-12-29 00:12:50 +0000101}
102
Chris Lattner88107952008-12-29 00:12:50 +0000103//===----------------------------------------------------------------------===//
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000104// PHINode Class
105//===----------------------------------------------------------------------===//
106
107PHINode::PHINode(const PHINode &PN)
Pete Cooper3fc30402015-06-10 22:38:46 +0000108 : Instruction(PN.getType(), Instruction::PHI, nullptr, PN.getNumOperands()),
109 ReservedSpace(PN.getNumOperands()) {
110 allocHungoffUses(PN.getNumOperands());
Jay Foad61ea0e42011-06-23 09:09:15 +0000111 std::copy(PN.op_begin(), PN.op_end(), op_begin());
112 std::copy(PN.block_begin(), PN.block_end(), block_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000113 SubclassOptionalData = PN.SubclassOptionalData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000114}
115
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000116// removeIncomingValue - Remove an incoming value. This is useful if a
117// predecessor basic block is deleted.
118Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
Jay Foad61ea0e42011-06-23 09:09:15 +0000119 Value *Removed = getIncomingValue(Idx);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000120
121 // Move everything after this operand down.
122 //
123 // FIXME: we could just swap with the end of the list, then erase. However,
Jay Foad61ea0e42011-06-23 09:09:15 +0000124 // clients might not expect this to happen. The code as it is thrashes the
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000125 // use/def lists, which is kinda lame.
Jay Foad61ea0e42011-06-23 09:09:15 +0000126 std::copy(op_begin() + Idx + 1, op_end(), op_begin() + Idx);
127 std::copy(block_begin() + Idx + 1, block_end(), block_begin() + Idx);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000128
129 // Nuke the last value.
Craig Topperc6207612014-04-09 06:08:46 +0000130 Op<-1>().set(nullptr);
Pete Cooperb4eede22015-06-12 17:48:10 +0000131 setNumHungOffUseOperands(getNumOperands() - 1);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000132
133 // If the PHI node is dead, because it has zero entries, nuke it now.
Jay Foad61ea0e42011-06-23 09:09:15 +0000134 if (getNumOperands() == 0 && DeletePHIIfEmpty) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000135 // If anyone is using this PHI, make them use a dummy value instead...
Owen Andersonb292b8c2009-07-30 23:03:37 +0000136 replaceAllUsesWith(UndefValue::get(getType()));
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000137 eraseFromParent();
138 }
139 return Removed;
140}
141
Jay Foade98f29d2011-04-01 08:00:58 +0000142/// growOperands - grow operands - This grows the operand list in response
143/// to a push_back style of operation. This grows the number of ops by 1.5
144/// times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000145///
Jay Foade98f29d2011-04-01 08:00:58 +0000146void PHINode::growOperands() {
Gabor Greiff6caff662008-05-10 08:32:32 +0000147 unsigned e = getNumOperands();
Jay Foad61ea0e42011-06-23 09:09:15 +0000148 unsigned NumOps = e + e / 2;
149 if (NumOps < 2) NumOps = 2; // 2 op PHI nodes are VERY common.
150
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000151 ReservedSpace = NumOps;
Pete Cooper93f9ff52015-06-10 22:38:41 +0000152 growHungoffUses(ReservedSpace, /* IsPhi */ true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000153}
154
Nate Begemanb3923212005-08-04 23:24:19 +0000155/// hasConstantValue - If the specified PHI node always merges together the same
156/// value, return the value, otherwise return null.
Duncan Sands7412f6e2010-11-17 04:30:22 +0000157Value *PHINode::hasConstantValue() const {
158 // Exploit the fact that phi nodes always have at least one entry.
159 Value *ConstantValue = getIncomingValue(0);
160 for (unsigned i = 1, e = getNumIncomingValues(); i != e; ++i)
Nuno Lopes90c76df2012-07-03 17:10:28 +0000161 if (getIncomingValue(i) != ConstantValue && getIncomingValue(i) != this) {
162 if (ConstantValue != this)
Craig Topperc6207612014-04-09 06:08:46 +0000163 return nullptr; // Incoming values not all the same.
Nuno Lopes90c76df2012-07-03 17:10:28 +0000164 // The case where the first value is this PHI.
165 ConstantValue = getIncomingValue(i);
166 }
Nuno Lopes0d44a502012-07-03 21:15:40 +0000167 if (ConstantValue == this)
168 return UndefValue::get(getType());
Duncan Sands7412f6e2010-11-17 04:30:22 +0000169 return ConstantValue;
Nate Begemanb3923212005-08-04 23:24:19 +0000170}
171
Nicolai Haehnle13d90f32016-04-14 17:42:47 +0000172/// hasConstantOrUndefValue - Whether the specified PHI node always merges
173/// together the same value, assuming that undefs result in the same value as
174/// non-undefs.
175/// Unlike \ref hasConstantValue, this does not return a value because the
176/// unique non-undef incoming value need not dominate the PHI node.
177bool PHINode::hasConstantOrUndefValue() const {
178 Value *ConstantValue = nullptr;
179 for (unsigned i = 0, e = getNumIncomingValues(); i != e; ++i) {
180 Value *Incoming = getIncomingValue(i);
181 if (Incoming != this && !isa<UndefValue>(Incoming)) {
182 if (ConstantValue && ConstantValue != Incoming)
183 return false;
184 ConstantValue = Incoming;
185 }
186 }
187 return true;
188}
189
Bill Wendlingfae14752011-08-12 20:24:12 +0000190//===----------------------------------------------------------------------===//
191// LandingPadInst Implementation
192//===----------------------------------------------------------------------===//
193
David Majnemer7fddecc2015-06-17 20:52:32 +0000194LandingPadInst::LandingPadInst(Type *RetTy, unsigned NumReservedValues,
195 const Twine &NameStr, Instruction *InsertBefore)
196 : Instruction(RetTy, Instruction::LandingPad, nullptr, 0, InsertBefore) {
197 init(NumReservedValues, NameStr);
Bill Wendlingfae14752011-08-12 20:24:12 +0000198}
199
David Majnemer7fddecc2015-06-17 20:52:32 +0000200LandingPadInst::LandingPadInst(Type *RetTy, unsigned NumReservedValues,
201 const Twine &NameStr, BasicBlock *InsertAtEnd)
202 : Instruction(RetTy, Instruction::LandingPad, nullptr, 0, InsertAtEnd) {
203 init(NumReservedValues, NameStr);
Bill Wendlingfae14752011-08-12 20:24:12 +0000204}
205
206LandingPadInst::LandingPadInst(const LandingPadInst &LP)
Pete Cooper3fc30402015-06-10 22:38:46 +0000207 : Instruction(LP.getType(), Instruction::LandingPad, nullptr,
208 LP.getNumOperands()),
209 ReservedSpace(LP.getNumOperands()) {
210 allocHungoffUses(LP.getNumOperands());
Pete Cooper74510a42015-06-12 17:48:05 +0000211 Use *OL = getOperandList();
212 const Use *InOL = LP.getOperandList();
Bill Wendlingfae14752011-08-12 20:24:12 +0000213 for (unsigned I = 0, E = ReservedSpace; I != E; ++I)
214 OL[I] = InOL[I];
215
216 setCleanup(LP.isCleanup());
217}
218
David Majnemer7fddecc2015-06-17 20:52:32 +0000219LandingPadInst *LandingPadInst::Create(Type *RetTy, unsigned NumReservedClauses,
Bill Wendlingfae14752011-08-12 20:24:12 +0000220 const Twine &NameStr,
221 Instruction *InsertBefore) {
David Majnemer7fddecc2015-06-17 20:52:32 +0000222 return new LandingPadInst(RetTy, NumReservedClauses, NameStr, InsertBefore);
Bill Wendlingfae14752011-08-12 20:24:12 +0000223}
224
David Majnemer7fddecc2015-06-17 20:52:32 +0000225LandingPadInst *LandingPadInst::Create(Type *RetTy, unsigned NumReservedClauses,
Bill Wendlingfae14752011-08-12 20:24:12 +0000226 const Twine &NameStr,
227 BasicBlock *InsertAtEnd) {
David Majnemer7fddecc2015-06-17 20:52:32 +0000228 return new LandingPadInst(RetTy, NumReservedClauses, NameStr, InsertAtEnd);
Bill Wendlingfae14752011-08-12 20:24:12 +0000229}
230
David Majnemer7fddecc2015-06-17 20:52:32 +0000231void LandingPadInst::init(unsigned NumReservedValues, const Twine &NameStr) {
Bill Wendlingfae14752011-08-12 20:24:12 +0000232 ReservedSpace = NumReservedValues;
David Majnemer7fddecc2015-06-17 20:52:32 +0000233 setNumHungOffUseOperands(0);
Pete Cooper3fc30402015-06-10 22:38:46 +0000234 allocHungoffUses(ReservedSpace);
Bill Wendlingfae14752011-08-12 20:24:12 +0000235 setName(NameStr);
236 setCleanup(false);
237}
238
239/// growOperands - grow operands - This grows the operand list in response to a
240/// push_back style of operation. This grows the number of ops by 2 times.
241void LandingPadInst::growOperands(unsigned Size) {
242 unsigned e = getNumOperands();
243 if (ReservedSpace >= e + Size) return;
David Majnemer7fddecc2015-06-17 20:52:32 +0000244 ReservedSpace = (std::max(e, 1U) + Size / 2) * 2;
Pete Cooper93f9ff52015-06-10 22:38:41 +0000245 growHungoffUses(ReservedSpace);
Bill Wendlingfae14752011-08-12 20:24:12 +0000246}
247
Rafael Espindola4dc5dfc2014-06-04 18:51:31 +0000248void LandingPadInst::addClause(Constant *Val) {
Bill Wendlingfae14752011-08-12 20:24:12 +0000249 unsigned OpNo = getNumOperands();
250 growOperands(1);
251 assert(OpNo < ReservedSpace && "Growing didn't work!");
Pete Cooperb4eede22015-06-12 17:48:10 +0000252 setNumHungOffUseOperands(getNumOperands() + 1);
Pete Cooper74510a42015-06-12 17:48:05 +0000253 getOperandList()[OpNo] = Val;
Bill Wendlingfae14752011-08-12 20:24:12 +0000254}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000255
256//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000257// CallInst Implementation
258//===----------------------------------------------------------------------===//
259
David Blaikie348de692015-04-23 21:36:23 +0000260void CallInst::init(FunctionType *FTy, Value *Func, ArrayRef<Value *> Args,
Sanjoy Das9303c242015-09-24 19:14:18 +0000261 ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr) {
David Blaikie348de692015-04-23 21:36:23 +0000262 this->FTy = FTy;
Sanjoy Das9303c242015-09-24 19:14:18 +0000263 assert(getNumOperands() == Args.size() + CountBundleInputs(Bundles) + 1 &&
264 "NumOperands not set up?");
Gabor Greif6d673952010-07-16 09:38:02 +0000265 Op<-1>() = Func;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000266
Jay Foad5bd375a2011-07-15 08:37:34 +0000267#ifndef NDEBUG
Jay Foad5bd375a2011-07-15 08:37:34 +0000268 assert((Args.size() == FTy->getNumParams() ||
269 (FTy->isVarArg() && Args.size() > FTy->getNumParams())) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000270 "Calling a function with bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000271
272 for (unsigned i = 0; i != Args.size(); ++i)
Fangrui Songf78650a2018-07-30 19:41:25 +0000273 assert((i >= FTy->getNumParams() ||
Jay Foad5bd375a2011-07-15 08:37:34 +0000274 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000275 "Calling a function with a bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000276#endif
277
Fangrui Song75709322018-11-17 01:44:25 +0000278 llvm::copy(Args, op_begin());
Sanjoy Das9303c242015-09-24 19:14:18 +0000279
280 auto It = populateBundleOperandInfos(Bundles, Args.size());
281 (void)It;
282 assert(It + 1 == op_end() && "Should add up!");
283
Jay Foad5bd375a2011-07-15 08:37:34 +0000284 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000285}
286
Jay Foad5bd375a2011-07-15 08:37:34 +0000287void CallInst::init(Value *Func, const Twine &NameStr) {
David Blaikie348de692015-04-23 21:36:23 +0000288 FTy =
289 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Pete Cooperb4eede22015-06-12 17:48:10 +0000290 assert(getNumOperands() == 1 && "NumOperands not set up?");
Gabor Greif6d673952010-07-16 09:38:02 +0000291 Op<-1>() = Func;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000292
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000293 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
Jay Foad5bd375a2011-07-15 08:37:34 +0000294
295 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000296}
297
Serge Guelton1fb81bc2018-02-22 13:30:32 +0000298CallInst::CallInst(Value *Func, const Twine &Name, Instruction *InsertBefore)
299 : CallBase<CallInst>(
300 cast<FunctionType>(
301 cast<PointerType>(Func->getType())->getElementType())
302 ->getReturnType(),
303 Instruction::Call,
304 OperandTraits<CallBase<CallInst>>::op_end(this) - 1, 1,
305 InsertBefore) {
Jay Foad5bd375a2011-07-15 08:37:34 +0000306 init(Func, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000307}
308
Serge Guelton1fb81bc2018-02-22 13:30:32 +0000309CallInst::CallInst(Value *Func, const Twine &Name, BasicBlock *InsertAtEnd)
310 : CallBase<CallInst>(
311 cast<FunctionType>(
312 cast<PointerType>(Func->getType())->getElementType())
313 ->getReturnType(),
314 Instruction::Call,
315 OperandTraits<CallBase<CallInst>>::op_end(this) - 1, 1, InsertAtEnd) {
Jay Foad5bd375a2011-07-15 08:37:34 +0000316 init(Func, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000317}
318
Misha Brukmanb1c93172005-04-21 23:48:37 +0000319CallInst::CallInst(const CallInst &CI)
Serge Guelton1fb81bc2018-02-22 13:30:32 +0000320 : CallBase<CallInst>(CI.Attrs, CI.FTy, CI.getType(), Instruction::Call,
321 OperandTraits<CallBase<CallInst>>::op_end(this) -
322 CI.getNumOperands(),
323 CI.getNumOperands()) {
Reid Kleckner118e1bf2014-05-06 20:08:20 +0000324 setTailCallKind(CI.getTailCallKind());
Chris Lattnerb9c86512009-12-29 02:14:09 +0000325 setCallingConv(CI.getCallingConv());
Sanjoy Das9303c242015-09-24 19:14:18 +0000326
Jay Foad5bd375a2011-07-15 08:37:34 +0000327 std::copy(CI.op_begin(), CI.op_end(), op_begin());
Sanjoy Das9303c242015-09-24 19:14:18 +0000328 std::copy(CI.bundle_op_info_begin(), CI.bundle_op_info_end(),
329 bundle_op_info_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000330 SubclassOptionalData = CI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000331}
332
Sanjoy Das2d161452015-11-18 06:23:38 +0000333CallInst *CallInst::Create(CallInst *CI, ArrayRef<OperandBundleDef> OpB,
334 Instruction *InsertPt) {
Sanjoy Dasccd14562015-12-10 06:39:02 +0000335 std::vector<Value *> Args(CI->arg_begin(), CI->arg_end());
Sanjoy Das2d161452015-11-18 06:23:38 +0000336
337 auto *NewCI = CallInst::Create(CI->getCalledValue(), Args, OpB, CI->getName(),
338 InsertPt);
339 NewCI->setTailCallKind(CI->getTailCallKind());
340 NewCI->setCallingConv(CI->getCallingConv());
341 NewCI->SubclassOptionalData = CI->SubclassOptionalData;
Sanjoy Dasb8dced52015-12-09 01:01:28 +0000342 NewCI->setAttributes(CI->getAttributes());
Joseph Tremouletbba70e42016-01-14 06:21:42 +0000343 NewCI->setDebugLoc(CI->getDebugLoc());
Sanjoy Das2d161452015-11-18 06:23:38 +0000344 return NewCI;
345}
346
Hal Finkele87ad542016-07-10 23:01:32 +0000347
Reid Klecknera0b45f42017-05-03 18:17:31 +0000348
Hal Finkele87ad542016-07-10 23:01:32 +0000349
Eric Christopher901b1a72008-05-16 20:39:43 +0000350
Amaury Secheta65a2372016-06-15 05:14:29 +0000351
Reid Kleckner5fbdd172017-05-31 19:23:09 +0000352
Reid Klecknera0b45f42017-05-03 18:17:31 +0000353
Amaury Sechet1a0e0972016-04-21 21:29:10 +0000354
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000355
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000356/// IsConstantOne - Return true only if val is constant int 1
357static bool IsConstantOne(Value *val) {
Reid Kleckner971c3ea2014-11-13 22:55:19 +0000358 assert(val && "IsConstantOne does not work with nullptr val");
Matt Arsenault69417852014-09-15 17:56:51 +0000359 const ConstantInt *CVal = dyn_cast<ConstantInt>(val);
360 return CVal && CVal->isOne();
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000361}
362
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000363static Instruction *createMalloc(Instruction *InsertBefore,
Chris Lattner229907c2011-07-18 04:54:35 +0000364 BasicBlock *InsertAtEnd, Type *IntPtrTy,
David Majnemerfadc6db2016-04-29 08:07:22 +0000365 Type *AllocTy, Value *AllocSize,
366 Value *ArraySize,
367 ArrayRef<OperandBundleDef> OpB,
368 Function *MallocF, const Twine &Name) {
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000369 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
Victor Hernandez788eaab2009-09-18 19:20:02 +0000370 "createMalloc needs either InsertBefore or InsertAtEnd");
371
Fangrui Songf78650a2018-07-30 19:41:25 +0000372 // malloc(type) becomes:
Victor Hernandez788eaab2009-09-18 19:20:02 +0000373 // bitcast (i8* malloc(typeSize)) to type*
374 // malloc(type, arraySize) becomes:
Ana Pazosb3596022016-02-03 21:34:39 +0000375 // bitcast (i8* malloc(typeSize*arraySize)) to type*
Victor Hernandezf3db9152009-11-07 00:16:28 +0000376 if (!ArraySize)
377 ArraySize = ConstantInt::get(IntPtrTy, 1);
378 else if (ArraySize->getType() != IntPtrTy) {
379 if (InsertBefore)
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000380 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
381 "", InsertBefore);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000382 else
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000383 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
384 "", InsertAtEnd);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000385 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000386
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000387 if (!IsConstantOne(ArraySize)) {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000388 if (IsConstantOne(AllocSize)) {
389 AllocSize = ArraySize; // Operand * 1 = Operand
390 } else if (Constant *CO = dyn_cast<Constant>(ArraySize)) {
391 Constant *Scale = ConstantExpr::getIntegerCast(CO, IntPtrTy,
392 false /*ZExt*/);
393 // Malloc arg is constant product of type size and array size
394 AllocSize = ConstantExpr::getMul(Scale, cast<Constant>(AllocSize));
395 } else {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000396 // Multiply type size by the array size...
397 if (InsertBefore)
Victor Hernandez788eaab2009-09-18 19:20:02 +0000398 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
399 "mallocsize", InsertBefore);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000400 else
Victor Hernandez788eaab2009-09-18 19:20:02 +0000401 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
402 "mallocsize", InsertAtEnd);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000403 }
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000404 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000405
Victor Hernandez788eaab2009-09-18 19:20:02 +0000406 assert(AllocSize->getType() == IntPtrTy && "malloc arg is wrong size");
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000407 // Create the call to Malloc.
Ana Pazosb3596022016-02-03 21:34:39 +0000408 BasicBlock *BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
409 Module *M = BB->getParent()->getParent();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000410 Type *BPTy = Type::getInt8PtrTy(BB->getContext());
Victor Hernandezbb336a12009-11-10 19:53:28 +0000411 Value *MallocFunc = MallocF;
412 if (!MallocFunc)
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000413 // prototype malloc as "void *malloc(size_t)"
Serge Guelton59a2d7b2017-04-11 15:01:18 +0000414 MallocFunc = M->getOrInsertFunction("malloc", BPTy, IntPtrTy);
Chris Lattner229907c2011-07-18 04:54:35 +0000415 PointerType *AllocPtrType = PointerType::getUnqual(AllocTy);
Craig Topperc6207612014-04-09 06:08:46 +0000416 CallInst *MCall = nullptr;
417 Instruction *Result = nullptr;
Victor Hernandez788eaab2009-09-18 19:20:02 +0000418 if (InsertBefore) {
David Majnemerfadc6db2016-04-29 08:07:22 +0000419 MCall = CallInst::Create(MallocFunc, AllocSize, OpB, "malloccall",
420 InsertBefore);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000421 Result = MCall;
422 if (Result->getType() != AllocPtrType)
423 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000424 Result = new BitCastInst(MCall, AllocPtrType, Name, InsertBefore);
Victor Hernandez788eaab2009-09-18 19:20:02 +0000425 } else {
David Majnemerfadc6db2016-04-29 08:07:22 +0000426 MCall = CallInst::Create(MallocFunc, AllocSize, OpB, "malloccall");
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000427 Result = MCall;
428 if (Result->getType() != AllocPtrType) {
429 InsertAtEnd->getInstList().push_back(MCall);
430 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000431 Result = new BitCastInst(MCall, AllocPtrType, Name);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000432 }
Victor Hernandez788eaab2009-09-18 19:20:02 +0000433 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000434 MCall->setTailCall();
Victor Hernandezbb336a12009-11-10 19:53:28 +0000435 if (Function *F = dyn_cast<Function>(MallocFunc)) {
436 MCall->setCallingConv(F->getCallingConv());
Reid Klecknera0b45f42017-05-03 18:17:31 +0000437 if (!F->returnDoesNotAlias())
438 F->setReturnDoesNotAlias();
Victor Hernandezbb336a12009-11-10 19:53:28 +0000439 }
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000440 assert(!MCall->getType()->isVoidTy() && "Malloc has void return type");
Victor Hernandez788eaab2009-09-18 19:20:02 +0000441
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000442 return Result;
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000443}
444
445/// CreateMalloc - Generate the IR for a call to malloc:
446/// 1. Compute the malloc call's argument as the specified type's size,
447/// possibly multiplied by the array size if the array size is not
448/// constant 1.
449/// 2. Call malloc with that argument.
450/// 3. Bitcast the result of the malloc call to the specified type.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000451Instruction *CallInst::CreateMalloc(Instruction *InsertBefore,
Chris Lattner229907c2011-07-18 04:54:35 +0000452 Type *IntPtrTy, Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000453 Value *AllocSize, Value *ArraySize,
Ana Pazosb3596022016-02-03 21:34:39 +0000454 Function *MallocF,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000455 const Twine &Name) {
Craig Topperc6207612014-04-09 06:08:46 +0000456 return createMalloc(InsertBefore, nullptr, IntPtrTy, AllocTy, AllocSize,
David Majnemerfadc6db2016-04-29 08:07:22 +0000457 ArraySize, None, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000458}
David Majnemerfadc6db2016-04-29 08:07:22 +0000459Instruction *CallInst::CreateMalloc(Instruction *InsertBefore,
460 Type *IntPtrTy, Type *AllocTy,
461 Value *AllocSize, Value *ArraySize,
462 ArrayRef<OperandBundleDef> OpB,
463 Function *MallocF,
464 const Twine &Name) {
465 return createMalloc(InsertBefore, nullptr, IntPtrTy, AllocTy, AllocSize,
466 ArraySize, OpB, MallocF, Name);
467}
468
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000469/// CreateMalloc - Generate the IR for a call to malloc:
470/// 1. Compute the malloc call's argument as the specified type's size,
471/// possibly multiplied by the array size if the array size is not
472/// constant 1.
473/// 2. Call malloc with that argument.
474/// 3. Bitcast the result of the malloc call to the specified type.
475/// Note: This function does not add the bitcast to the basic block, that is the
476/// responsibility of the caller.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000477Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd,
Chris Lattner229907c2011-07-18 04:54:35 +0000478 Type *IntPtrTy, Type *AllocTy,
Fangrui Songf78650a2018-07-30 19:41:25 +0000479 Value *AllocSize, Value *ArraySize,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000480 Function *MallocF, const Twine &Name) {
Craig Topperc6207612014-04-09 06:08:46 +0000481 return createMalloc(nullptr, InsertAtEnd, IntPtrTy, AllocTy, AllocSize,
David Majnemerfadc6db2016-04-29 08:07:22 +0000482 ArraySize, None, MallocF, Name);
483}
484Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd,
485 Type *IntPtrTy, Type *AllocTy,
486 Value *AllocSize, Value *ArraySize,
487 ArrayRef<OperandBundleDef> OpB,
488 Function *MallocF, const Twine &Name) {
489 return createMalloc(nullptr, InsertAtEnd, IntPtrTy, AllocTy, AllocSize,
490 ArraySize, OpB, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000491}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000492
David Majnemerfadc6db2016-04-29 08:07:22 +0000493static Instruction *createFree(Value *Source,
494 ArrayRef<OperandBundleDef> Bundles,
495 Instruction *InsertBefore,
Victor Hernandeze2971492009-10-24 04:23:03 +0000496 BasicBlock *InsertAtEnd) {
497 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
498 "createFree needs either InsertBefore or InsertAtEnd");
Duncan Sands19d0b472010-02-16 11:11:14 +0000499 assert(Source->getType()->isPointerTy() &&
Victor Hernandeze2971492009-10-24 04:23:03 +0000500 "Can not free something of nonpointer type!");
501
Ana Pazosb3596022016-02-03 21:34:39 +0000502 BasicBlock *BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
503 Module *M = BB->getParent()->getParent();
Victor Hernandeze2971492009-10-24 04:23:03 +0000504
Chris Lattner229907c2011-07-18 04:54:35 +0000505 Type *VoidTy = Type::getVoidTy(M->getContext());
506 Type *IntPtrTy = Type::getInt8PtrTy(M->getContext());
Victor Hernandeze2971492009-10-24 04:23:03 +0000507 // prototype free as "void free(void*)"
Serge Guelton59a2d7b2017-04-11 15:01:18 +0000508 Value *FreeFunc = M->getOrInsertFunction("free", VoidTy, IntPtrTy);
Ana Pazosb3596022016-02-03 21:34:39 +0000509 CallInst *Result = nullptr;
Victor Hernandeze2971492009-10-24 04:23:03 +0000510 Value *PtrCast = Source;
511 if (InsertBefore) {
512 if (Source->getType() != IntPtrTy)
513 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertBefore);
David Majnemerfadc6db2016-04-29 08:07:22 +0000514 Result = CallInst::Create(FreeFunc, PtrCast, Bundles, "", InsertBefore);
Victor Hernandeze2971492009-10-24 04:23:03 +0000515 } else {
516 if (Source->getType() != IntPtrTy)
517 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertAtEnd);
David Majnemerfadc6db2016-04-29 08:07:22 +0000518 Result = CallInst::Create(FreeFunc, PtrCast, Bundles, "");
Victor Hernandeze2971492009-10-24 04:23:03 +0000519 }
520 Result->setTailCall();
Chris Lattner2156c222009-11-09 07:12:01 +0000521 if (Function *F = dyn_cast<Function>(FreeFunc))
522 Result->setCallingConv(F->getCallingConv());
Victor Hernandeze2971492009-10-24 04:23:03 +0000523
524 return Result;
525}
526
527/// CreateFree - Generate the IR for a call to the builtin free function.
Ana Pazosb3596022016-02-03 21:34:39 +0000528Instruction *CallInst::CreateFree(Value *Source, Instruction *InsertBefore) {
David Majnemerfadc6db2016-04-29 08:07:22 +0000529 return createFree(Source, None, InsertBefore, nullptr);
530}
531Instruction *CallInst::CreateFree(Value *Source,
532 ArrayRef<OperandBundleDef> Bundles,
533 Instruction *InsertBefore) {
534 return createFree(Source, Bundles, InsertBefore, nullptr);
Victor Hernandeze2971492009-10-24 04:23:03 +0000535}
536
537/// CreateFree - Generate the IR for a call to the builtin free function.
538/// Note: This function does not add the call to the basic block, that is the
539/// responsibility of the caller.
Ana Pazosb3596022016-02-03 21:34:39 +0000540Instruction *CallInst::CreateFree(Value *Source, BasicBlock *InsertAtEnd) {
David Majnemerfadc6db2016-04-29 08:07:22 +0000541 Instruction *FreeCall = createFree(Source, None, nullptr, InsertAtEnd);
542 assert(FreeCall && "CreateFree did not create a CallInst");
543 return FreeCall;
544}
545Instruction *CallInst::CreateFree(Value *Source,
546 ArrayRef<OperandBundleDef> Bundles,
547 BasicBlock *InsertAtEnd) {
548 Instruction *FreeCall = createFree(Source, Bundles, nullptr, InsertAtEnd);
Victor Hernandeze2971492009-10-24 04:23:03 +0000549 assert(FreeCall && "CreateFree did not create a CallInst");
550 return FreeCall;
551}
552
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000553//===----------------------------------------------------------------------===//
554// InvokeInst Implementation
555//===----------------------------------------------------------------------===//
556
David Blaikie3e807092015-05-13 18:35:26 +0000557void InvokeInst::init(FunctionType *FTy, Value *Fn, BasicBlock *IfNormal,
558 BasicBlock *IfException, ArrayRef<Value *> Args,
Sanjoy Das9303c242015-09-24 19:14:18 +0000559 ArrayRef<OperandBundleDef> Bundles,
David Blaikie3e807092015-05-13 18:35:26 +0000560 const Twine &NameStr) {
561 this->FTy = FTy;
David Blaikie348de692015-04-23 21:36:23 +0000562
Sanjoy Das9303c242015-09-24 19:14:18 +0000563 assert(getNumOperands() == 3 + Args.size() + CountBundleInputs(Bundles) &&
564 "NumOperands not set up?");
Gabor Greifa2fbc0a2010-03-24 13:21:49 +0000565 Op<-3>() = Fn;
566 Op<-2>() = IfNormal;
567 Op<-1>() = IfException;
Jay Foad5bd375a2011-07-15 08:37:34 +0000568
569#ifndef NDEBUG
Jay Foad5bd375a2011-07-15 08:37:34 +0000570 assert(((Args.size() == FTy->getNumParams()) ||
571 (FTy->isVarArg() && Args.size() > FTy->getNumParams())) &&
Gabor Greif668d7002010-03-23 13:45:54 +0000572 "Invoking a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000573
Jay Foad5bd375a2011-07-15 08:37:34 +0000574 for (unsigned i = 0, e = Args.size(); i != e; i++)
Fangrui Songf78650a2018-07-30 19:41:25 +0000575 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000576 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000577 "Invoking a function with a bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000578#endif
579
Fangrui Song75709322018-11-17 01:44:25 +0000580 llvm::copy(Args, op_begin());
Sanjoy Das9303c242015-09-24 19:14:18 +0000581
582 auto It = populateBundleOperandInfos(Bundles, Args.size());
583 (void)It;
584 assert(It + 3 == op_end() && "Should add up!");
585
Jay Foad5bd375a2011-07-15 08:37:34 +0000586 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000587}
588
Misha Brukmanb1c93172005-04-21 23:48:37 +0000589InvokeInst::InvokeInst(const InvokeInst &II)
Serge Guelton1fb81bc2018-02-22 13:30:32 +0000590 : CallBase<InvokeInst>(II.Attrs, II.FTy, II.getType(), Instruction::Invoke,
591 OperandTraits<CallBase<InvokeInst>>::op_end(this) -
592 II.getNumOperands(),
593 II.getNumOperands()) {
Chris Lattnerb9c86512009-12-29 02:14:09 +0000594 setCallingConv(II.getCallingConv());
Jay Foad5bd375a2011-07-15 08:37:34 +0000595 std::copy(II.op_begin(), II.op_end(), op_begin());
Sanjoy Das9303c242015-09-24 19:14:18 +0000596 std::copy(II.bundle_op_info_begin(), II.bundle_op_info_end(),
597 bundle_op_info_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000598 SubclassOptionalData = II.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000599}
600
Sanjoy Das2d161452015-11-18 06:23:38 +0000601InvokeInst *InvokeInst::Create(InvokeInst *II, ArrayRef<OperandBundleDef> OpB,
602 Instruction *InsertPt) {
Sanjoy Dasccd14562015-12-10 06:39:02 +0000603 std::vector<Value *> Args(II->arg_begin(), II->arg_end());
Sanjoy Das2d161452015-11-18 06:23:38 +0000604
605 auto *NewII = InvokeInst::Create(II->getCalledValue(), II->getNormalDest(),
606 II->getUnwindDest(), Args, OpB,
607 II->getName(), InsertPt);
608 NewII->setCallingConv(II->getCallingConv());
609 NewII->SubclassOptionalData = II->SubclassOptionalData;
Sanjoy Dasb8dced52015-12-09 01:01:28 +0000610 NewII->setAttributes(II->getAttributes());
Joseph Tremouletbba70e42016-01-14 06:21:42 +0000611 NewII->setDebugLoc(II->getDebugLoc());
Sanjoy Das2d161452015-11-18 06:23:38 +0000612 return NewII;
613}
614
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000615
Bill Wendlingfae14752011-08-12 20:24:12 +0000616LandingPadInst *InvokeInst::getLandingPadInst() const {
617 return cast<LandingPadInst>(getUnwindDest()->getFirstNonPHI());
618}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000619
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000620//===----------------------------------------------------------------------===//
621// ReturnInst Implementation
622//===----------------------------------------------------------------------===//
623
Chris Lattner2195fc42007-02-24 00:55:48 +0000624ReturnInst::ReturnInst(const ReturnInst &RI)
Chandler Carruth509e20e2018-10-19 00:22:37 +0000625 : Instruction(Type::getVoidTy(RI.getContext()), Instruction::Ret,
626 OperandTraits<ReturnInst>::op_end(this) - RI.getNumOperands(),
627 RI.getNumOperands()) {
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000628 if (RI.getNumOperands())
Gabor Greif2d3024d2008-05-26 21:33:52 +0000629 Op<0>() = RI.Op<0>();
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000630 SubclassOptionalData = RI.SubclassOptionalData;
Chris Lattner2195fc42007-02-24 00:55:48 +0000631}
632
Owen Anderson55f1c092009-08-13 21:58:54 +0000633ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, Instruction *InsertBefore)
Chandler Carruth509e20e2018-10-19 00:22:37 +0000634 : Instruction(Type::getVoidTy(C), Instruction::Ret,
635 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
636 InsertBefore) {
Devang Patelc38eb522008-02-26 18:49:29 +0000637 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000638 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000639}
Eugene Zelenkod761e2c2017-05-15 21:57:41 +0000640
Owen Anderson55f1c092009-08-13 21:58:54 +0000641ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd)
Chandler Carruth509e20e2018-10-19 00:22:37 +0000642 : Instruction(Type::getVoidTy(C), Instruction::Ret,
643 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
644 InsertAtEnd) {
Devang Patelc38eb522008-02-26 18:49:29 +0000645 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000646 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000647}
Eugene Zelenkod761e2c2017-05-15 21:57:41 +0000648
Owen Anderson55f1c092009-08-13 21:58:54 +0000649ReturnInst::ReturnInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
Chandler Carruth509e20e2018-10-19 00:22:37 +0000650 : Instruction(Type::getVoidTy(Context), Instruction::Ret,
651 OperandTraits<ReturnInst>::op_end(this), 0, InsertAtEnd) {}
Devang Patel59643e52008-02-23 00:35:18 +0000652
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000653//===----------------------------------------------------------------------===//
Bill Wendlingf891bf82011-07-31 06:30:59 +0000654// ResumeInst Implementation
655//===----------------------------------------------------------------------===//
656
657ResumeInst::ResumeInst(const ResumeInst &RI)
Chandler Carruth509e20e2018-10-19 00:22:37 +0000658 : Instruction(Type::getVoidTy(RI.getContext()), Instruction::Resume,
659 OperandTraits<ResumeInst>::op_begin(this), 1) {
Bill Wendlingf891bf82011-07-31 06:30:59 +0000660 Op<0>() = RI.Op<0>();
661}
662
663ResumeInst::ResumeInst(Value *Exn, Instruction *InsertBefore)
Chandler Carruth509e20e2018-10-19 00:22:37 +0000664 : Instruction(Type::getVoidTy(Exn->getContext()), Instruction::Resume,
665 OperandTraits<ResumeInst>::op_begin(this), 1, InsertBefore) {
Bill Wendlingf891bf82011-07-31 06:30:59 +0000666 Op<0>() = Exn;
667}
668
669ResumeInst::ResumeInst(Value *Exn, BasicBlock *InsertAtEnd)
Chandler Carruth509e20e2018-10-19 00:22:37 +0000670 : Instruction(Type::getVoidTy(Exn->getContext()), Instruction::Resume,
671 OperandTraits<ResumeInst>::op_begin(this), 1, InsertAtEnd) {
Bill Wendlingf891bf82011-07-31 06:30:59 +0000672 Op<0>() = Exn;
673}
674
Bill Wendlingf891bf82011-07-31 06:30:59 +0000675//===----------------------------------------------------------------------===//
David Majnemer654e1302015-07-31 17:58:14 +0000676// CleanupReturnInst Implementation
677//===----------------------------------------------------------------------===//
678
679CleanupReturnInst::CleanupReturnInst(const CleanupReturnInst &CRI)
Chandler Carruth509e20e2018-10-19 00:22:37 +0000680 : Instruction(CRI.getType(), Instruction::CleanupRet,
681 OperandTraits<CleanupReturnInst>::op_end(this) -
682 CRI.getNumOperands(),
683 CRI.getNumOperands()) {
David Majnemereb518bd2015-08-04 08:21:40 +0000684 setInstructionSubclassData(CRI.getSubclassDataFromInstruction());
David Majnemer8a1c45d2015-12-12 05:38:55 +0000685 Op<0>() = CRI.Op<0>();
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000686 if (CRI.hasUnwindDest())
David Majnemer8a1c45d2015-12-12 05:38:55 +0000687 Op<1>() = CRI.Op<1>();
David Majnemer654e1302015-07-31 17:58:14 +0000688}
689
David Majnemer8a1c45d2015-12-12 05:38:55 +0000690void CleanupReturnInst::init(Value *CleanupPad, BasicBlock *UnwindBB) {
David Majnemer654e1302015-07-31 17:58:14 +0000691 if (UnwindBB)
692 setInstructionSubclassData(getSubclassDataFromInstruction() | 1);
David Majnemer654e1302015-07-31 17:58:14 +0000693
David Majnemer8a1c45d2015-12-12 05:38:55 +0000694 Op<0>() = CleanupPad;
David Majnemer654e1302015-07-31 17:58:14 +0000695 if (UnwindBB)
David Majnemer8a1c45d2015-12-12 05:38:55 +0000696 Op<1>() = UnwindBB;
David Majnemer654e1302015-07-31 17:58:14 +0000697}
698
David Majnemer8a1c45d2015-12-12 05:38:55 +0000699CleanupReturnInst::CleanupReturnInst(Value *CleanupPad, BasicBlock *UnwindBB,
700 unsigned Values, Instruction *InsertBefore)
Chandler Carruth509e20e2018-10-19 00:22:37 +0000701 : Instruction(Type::getVoidTy(CleanupPad->getContext()),
702 Instruction::CleanupRet,
703 OperandTraits<CleanupReturnInst>::op_end(this) - Values,
704 Values, InsertBefore) {
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000705 init(CleanupPad, UnwindBB);
David Majnemer654e1302015-07-31 17:58:14 +0000706}
707
David Majnemer8a1c45d2015-12-12 05:38:55 +0000708CleanupReturnInst::CleanupReturnInst(Value *CleanupPad, BasicBlock *UnwindBB,
709 unsigned Values, BasicBlock *InsertAtEnd)
Chandler Carruth509e20e2018-10-19 00:22:37 +0000710 : Instruction(Type::getVoidTy(CleanupPad->getContext()),
711 Instruction::CleanupRet,
712 OperandTraits<CleanupReturnInst>::op_end(this) - Values,
713 Values, InsertAtEnd) {
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000714 init(CleanupPad, UnwindBB);
David Majnemer654e1302015-07-31 17:58:14 +0000715}
716
David Majnemer654e1302015-07-31 17:58:14 +0000717//===----------------------------------------------------------------------===//
David Majnemer654e1302015-07-31 17:58:14 +0000718// CatchReturnInst Implementation
719//===----------------------------------------------------------------------===//
David Majnemer8a1c45d2015-12-12 05:38:55 +0000720void CatchReturnInst::init(Value *CatchPad, BasicBlock *BB) {
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000721 Op<0>() = CatchPad;
722 Op<1>() = BB;
David Majnemer0bc0eef2015-08-15 02:46:08 +0000723}
David Majnemer654e1302015-07-31 17:58:14 +0000724
725CatchReturnInst::CatchReturnInst(const CatchReturnInst &CRI)
Chandler Carruth509e20e2018-10-19 00:22:37 +0000726 : Instruction(Type::getVoidTy(CRI.getContext()), Instruction::CatchRet,
727 OperandTraits<CatchReturnInst>::op_begin(this), 2) {
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000728 Op<0>() = CRI.Op<0>();
729 Op<1>() = CRI.Op<1>();
David Majnemer654e1302015-07-31 17:58:14 +0000730}
731
David Majnemer8a1c45d2015-12-12 05:38:55 +0000732CatchReturnInst::CatchReturnInst(Value *CatchPad, BasicBlock *BB,
David Majnemer0bc0eef2015-08-15 02:46:08 +0000733 Instruction *InsertBefore)
Chandler Carruth509e20e2018-10-19 00:22:37 +0000734 : Instruction(Type::getVoidTy(BB->getContext()), Instruction::CatchRet,
735 OperandTraits<CatchReturnInst>::op_begin(this), 2,
736 InsertBefore) {
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000737 init(CatchPad, BB);
David Majnemer654e1302015-07-31 17:58:14 +0000738}
739
David Majnemer8a1c45d2015-12-12 05:38:55 +0000740CatchReturnInst::CatchReturnInst(Value *CatchPad, BasicBlock *BB,
David Majnemer0bc0eef2015-08-15 02:46:08 +0000741 BasicBlock *InsertAtEnd)
Chandler Carruth509e20e2018-10-19 00:22:37 +0000742 : Instruction(Type::getVoidTy(BB->getContext()), Instruction::CatchRet,
743 OperandTraits<CatchReturnInst>::op_begin(this), 2,
744 InsertAtEnd) {
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000745 init(CatchPad, BB);
David Majnemer654e1302015-07-31 17:58:14 +0000746}
747
David Majnemer654e1302015-07-31 17:58:14 +0000748//===----------------------------------------------------------------------===//
David Majnemer8a1c45d2015-12-12 05:38:55 +0000749// CatchSwitchInst Implementation
David Majnemer654e1302015-07-31 17:58:14 +0000750//===----------------------------------------------------------------------===//
David Majnemer8a1c45d2015-12-12 05:38:55 +0000751
752CatchSwitchInst::CatchSwitchInst(Value *ParentPad, BasicBlock *UnwindDest,
753 unsigned NumReservedValues,
754 const Twine &NameStr,
755 Instruction *InsertBefore)
Chandler Carruth509e20e2018-10-19 00:22:37 +0000756 : Instruction(ParentPad->getType(), Instruction::CatchSwitch, nullptr, 0,
757 InsertBefore) {
David Majnemer8a1c45d2015-12-12 05:38:55 +0000758 if (UnwindDest)
759 ++NumReservedValues;
760 init(ParentPad, UnwindDest, NumReservedValues + 1);
David Majnemer654e1302015-07-31 17:58:14 +0000761 setName(NameStr);
762}
763
David Majnemer8a1c45d2015-12-12 05:38:55 +0000764CatchSwitchInst::CatchSwitchInst(Value *ParentPad, BasicBlock *UnwindDest,
765 unsigned NumReservedValues,
766 const Twine &NameStr, BasicBlock *InsertAtEnd)
Chandler Carruth509e20e2018-10-19 00:22:37 +0000767 : Instruction(ParentPad->getType(), Instruction::CatchSwitch, nullptr, 0,
768 InsertAtEnd) {
David Majnemer8a1c45d2015-12-12 05:38:55 +0000769 if (UnwindDest)
770 ++NumReservedValues;
771 init(ParentPad, UnwindDest, NumReservedValues + 1);
772 setName(NameStr);
David Majnemer654e1302015-07-31 17:58:14 +0000773}
774
David Majnemer8a1c45d2015-12-12 05:38:55 +0000775CatchSwitchInst::CatchSwitchInst(const CatchSwitchInst &CSI)
Chandler Carruth509e20e2018-10-19 00:22:37 +0000776 : Instruction(CSI.getType(), Instruction::CatchSwitch, nullptr,
777 CSI.getNumOperands()) {
David Majnemer8a1c45d2015-12-12 05:38:55 +0000778 init(CSI.getParentPad(), CSI.getUnwindDest(), CSI.getNumOperands());
779 setNumHungOffUseOperands(ReservedSpace);
780 Use *OL = getOperandList();
781 const Use *InOL = CSI.getOperandList();
782 for (unsigned I = 1, E = ReservedSpace; I != E; ++I)
783 OL[I] = InOL[I];
David Majnemer654e1302015-07-31 17:58:14 +0000784}
David Majnemer8a1c45d2015-12-12 05:38:55 +0000785
786void CatchSwitchInst::init(Value *ParentPad, BasicBlock *UnwindDest,
787 unsigned NumReservedValues) {
788 assert(ParentPad && NumReservedValues);
789
790 ReservedSpace = NumReservedValues;
791 setNumHungOffUseOperands(UnwindDest ? 2 : 1);
792 allocHungoffUses(ReservedSpace);
793
794 Op<0>() = ParentPad;
795 if (UnwindDest) {
796 setInstructionSubclassData(getSubclassDataFromInstruction() | 1);
797 setUnwindDest(UnwindDest);
798 }
799}
800
801/// growOperands - grow operands - This grows the operand list in response to a
802/// push_back style of operation. This grows the number of ops by 2 times.
803void CatchSwitchInst::growOperands(unsigned Size) {
804 unsigned NumOperands = getNumOperands();
805 assert(NumOperands >= 1);
806 if (ReservedSpace >= NumOperands + Size)
807 return;
808 ReservedSpace = (NumOperands + Size / 2) * 2;
809 growHungoffUses(ReservedSpace);
810}
811
812void CatchSwitchInst::addHandler(BasicBlock *Handler) {
813 unsigned OpNo = getNumOperands();
814 growOperands(1);
815 assert(OpNo < ReservedSpace && "Growing didn't work!");
816 setNumHungOffUseOperands(getNumOperands() + 1);
817 getOperandList()[OpNo] = Handler;
818}
819
Joseph Tremoulet0d808882016-01-05 02:37:41 +0000820void CatchSwitchInst::removeHandler(handler_iterator HI) {
821 // Move all subsequent handlers up one.
822 Use *EndDst = op_end() - 1;
823 for (Use *CurDst = HI.getCurrent(); CurDst != EndDst; ++CurDst)
824 *CurDst = *(CurDst + 1);
825 // Null out the last handler use.
826 *EndDst = nullptr;
827
828 setNumHungOffUseOperands(getNumOperands() - 1);
829}
830
David Majnemer8a1c45d2015-12-12 05:38:55 +0000831//===----------------------------------------------------------------------===//
832// FuncletPadInst Implementation
833//===----------------------------------------------------------------------===//
834void FuncletPadInst::init(Value *ParentPad, ArrayRef<Value *> Args,
835 const Twine &NameStr) {
836 assert(getNumOperands() == 1 + Args.size() && "NumOperands not set up?");
Fangrui Song75709322018-11-17 01:44:25 +0000837 llvm::copy(Args, op_begin());
David Majnemer8a1c45d2015-12-12 05:38:55 +0000838 setParentPad(ParentPad);
839 setName(NameStr);
840}
841
842FuncletPadInst::FuncletPadInst(const FuncletPadInst &FPI)
843 : Instruction(FPI.getType(), FPI.getOpcode(),
844 OperandTraits<FuncletPadInst>::op_end(this) -
845 FPI.getNumOperands(),
846 FPI.getNumOperands()) {
847 std::copy(FPI.op_begin(), FPI.op_end(), op_begin());
848 setParentPad(FPI.getParentPad());
849}
850
851FuncletPadInst::FuncletPadInst(Instruction::FuncletPadOps Op, Value *ParentPad,
852 ArrayRef<Value *> Args, unsigned Values,
853 const Twine &NameStr, Instruction *InsertBefore)
854 : Instruction(ParentPad->getType(), Op,
855 OperandTraits<FuncletPadInst>::op_end(this) - Values, Values,
856 InsertBefore) {
857 init(ParentPad, Args, NameStr);
858}
859
860FuncletPadInst::FuncletPadInst(Instruction::FuncletPadOps Op, Value *ParentPad,
861 ArrayRef<Value *> Args, unsigned Values,
862 const Twine &NameStr, BasicBlock *InsertAtEnd)
863 : Instruction(ParentPad->getType(), Op,
864 OperandTraits<FuncletPadInst>::op_end(this) - Values, Values,
865 InsertAtEnd) {
866 init(ParentPad, Args, NameStr);
David Majnemer654e1302015-07-31 17:58:14 +0000867}
868
869//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000870// UnreachableInst Implementation
871//===----------------------------------------------------------------------===//
872
Fangrui Songf78650a2018-07-30 19:41:25 +0000873UnreachableInst::UnreachableInst(LLVMContext &Context,
Owen Anderson55f1c092009-08-13 21:58:54 +0000874 Instruction *InsertBefore)
Chandler Carruth509e20e2018-10-19 00:22:37 +0000875 : Instruction(Type::getVoidTy(Context), Instruction::Unreachable, nullptr,
876 0, InsertBefore) {}
Owen Anderson55f1c092009-08-13 21:58:54 +0000877UnreachableInst::UnreachableInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
Chandler Carruth509e20e2018-10-19 00:22:37 +0000878 : Instruction(Type::getVoidTy(Context), Instruction::Unreachable, nullptr,
879 0, InsertAtEnd) {}
Chris Lattner2195fc42007-02-24 00:55:48 +0000880
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000881//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000882// BranchInst Implementation
883//===----------------------------------------------------------------------===//
884
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000885void BranchInst::AssertOK() {
886 if (isConditional())
Duncan Sands9dff9be2010-02-15 16:12:20 +0000887 assert(getCondition()->getType()->isIntegerTy(1) &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000888 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000889}
890
Chris Lattner2195fc42007-02-24 00:55:48 +0000891BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
Chandler Carruth509e20e2018-10-19 00:22:37 +0000892 : Instruction(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
893 OperandTraits<BranchInst>::op_end(this) - 1, 1,
894 InsertBefore) {
Craig Topper2617dcc2014-04-15 06:32:26 +0000895 assert(IfTrue && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000896 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000897}
Eugene Zelenkod761e2c2017-05-15 21:57:41 +0000898
Chris Lattner2195fc42007-02-24 00:55:48 +0000899BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
900 Instruction *InsertBefore)
Chandler Carruth509e20e2018-10-19 00:22:37 +0000901 : Instruction(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
902 OperandTraits<BranchInst>::op_end(this) - 3, 3,
903 InsertBefore) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000904 Op<-1>() = IfTrue;
905 Op<-2>() = IfFalse;
906 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000907#ifndef NDEBUG
908 AssertOK();
909#endif
910}
911
912BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
Chandler Carruth509e20e2018-10-19 00:22:37 +0000913 : Instruction(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
914 OperandTraits<BranchInst>::op_end(this) - 1, 1, InsertAtEnd) {
Craig Topper2617dcc2014-04-15 06:32:26 +0000915 assert(IfTrue && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000916 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000917}
918
919BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
Chandler Carruth509e20e2018-10-19 00:22:37 +0000920 BasicBlock *InsertAtEnd)
921 : Instruction(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
922 OperandTraits<BranchInst>::op_end(this) - 3, 3, InsertAtEnd) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000923 Op<-1>() = IfTrue;
924 Op<-2>() = IfFalse;
925 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000926#ifndef NDEBUG
927 AssertOK();
928#endif
929}
930
Chandler Carruth509e20e2018-10-19 00:22:37 +0000931BranchInst::BranchInst(const BranchInst &BI)
932 : Instruction(Type::getVoidTy(BI.getContext()), Instruction::Br,
933 OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(),
934 BI.getNumOperands()) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000935 Op<-1>() = BI.Op<-1>();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000936 if (BI.getNumOperands() != 1) {
937 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000938 Op<-3>() = BI.Op<-3>();
939 Op<-2>() = BI.Op<-2>();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000940 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000941 SubclassOptionalData = BI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000942}
943
Chandler Carruth3e8aa652011-10-17 01:11:57 +0000944void BranchInst::swapSuccessors() {
945 assert(isConditional() &&
946 "Cannot swap successors of an unconditional branch");
947 Op<-1>().swap(Op<-2>());
948
949 // Update profile metadata if present and it matches our structural
950 // expectations.
Xinliang David Lidc491402016-08-23 15:39:03 +0000951 swapProfMetadata();
Chandler Carruth3e8aa652011-10-17 01:11:57 +0000952}
953
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000954//===----------------------------------------------------------------------===//
Victor Hernandez8acf2952009-10-23 21:09:37 +0000955// AllocaInst Implementation
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000956//===----------------------------------------------------------------------===//
957
Owen Andersonb6b25302009-07-14 23:09:55 +0000958static Value *getAISize(LLVMContext &Context, Value *Amt) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000959 if (!Amt)
Owen Anderson55f1c092009-08-13 21:58:54 +0000960 Amt = ConstantInt::get(Type::getInt32Ty(Context), 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000961 else {
962 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +0000963 "Passed basic block into allocation size parameter! Use other ctor");
Dan Gohman2140a742010-05-28 01:14:11 +0000964 assert(Amt->getType()->isIntegerTy() &&
965 "Allocation array size is not an integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000966 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000967 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000968}
969
Matt Arsenault3c1fc762017-04-10 22:27:50 +0000970AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, const Twine &Name,
Victor Hernandez8acf2952009-10-23 21:09:37 +0000971 Instruction *InsertBefore)
Matt Arsenault3c1fc762017-04-10 22:27:50 +0000972 : AllocaInst(Ty, AddrSpace, /*ArraySize=*/nullptr, Name, InsertBefore) {}
Victor Hernandez8acf2952009-10-23 21:09:37 +0000973
Matt Arsenault3c1fc762017-04-10 22:27:50 +0000974AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, const Twine &Name,
Victor Hernandez8acf2952009-10-23 21:09:37 +0000975 BasicBlock *InsertAtEnd)
Matt Arsenault3c1fc762017-04-10 22:27:50 +0000976 : AllocaInst(Ty, AddrSpace, /*ArraySize=*/nullptr, Name, InsertAtEnd) {}
Victor Hernandez8acf2952009-10-23 21:09:37 +0000977
Matt Arsenault3c1fc762017-04-10 22:27:50 +0000978AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize,
Victor Hernandez8acf2952009-10-23 21:09:37 +0000979 const Twine &Name, Instruction *InsertBefore)
Matt Arsenault3c1fc762017-04-10 22:27:50 +0000980 : AllocaInst(Ty, AddrSpace, ArraySize, /*Align=*/0, Name, InsertBefore) {}
981
982AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize,
983 const Twine &Name, BasicBlock *InsertAtEnd)
984 : AllocaInst(Ty, AddrSpace, ArraySize, /*Align=*/0, Name, InsertAtEnd) {}
985
986AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize,
987 unsigned Align, const Twine &Name,
988 Instruction *InsertBefore)
989 : UnaryInstruction(PointerType::get(Ty, AddrSpace), Alloca,
990 getAISize(Ty->getContext(), ArraySize), InsertBefore),
991 AllocatedType(Ty) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000992 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000993 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000994 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000995}
996
Matt Arsenault3c1fc762017-04-10 22:27:50 +0000997AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize,
998 unsigned Align, const Twine &Name,
999 BasicBlock *InsertAtEnd)
1000 : UnaryInstruction(PointerType::get(Ty, AddrSpace), Alloca,
1001 getAISize(Ty->getContext(), ArraySize), InsertAtEnd),
David Blaikiebf0a42a2015-04-29 23:00:35 +00001002 AllocatedType(Ty) {
Dan Gohmanaa583d72008-03-24 16:55:58 +00001003 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +00001004 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +00001005 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001006}
1007
Victor Hernandez8acf2952009-10-23 21:09:37 +00001008void AllocaInst::setAlignment(unsigned Align) {
Dan Gohmanaa583d72008-03-24 16:55:58 +00001009 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001010 assert(Align <= MaximumAlignment &&
1011 "Alignment is greater than MaximumAlignment!");
Reid Kleckner436c42e2014-01-17 23:58:17 +00001012 setInstructionSubclassData((getSubclassDataFromInstruction() & ~31) |
1013 (Log2_32(Align) + 1));
Dan Gohmanaa583d72008-03-24 16:55:58 +00001014 assert(getAlignment() == Align && "Alignment representation error!");
1015}
1016
Victor Hernandez8acf2952009-10-23 21:09:37 +00001017bool AllocaInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +00001018 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
Dan Gohman9a1b8592010-09-27 15:15:44 +00001019 return !CI->isOne();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001020 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001021}
1022
Chris Lattner8b291e62008-11-26 02:54:17 +00001023/// isStaticAlloca - Return true if this alloca is in the entry block of the
1024/// function and is a constant size. If so, the code generator will fold it
1025/// into the prolog/epilog code, so it is basically free.
1026bool AllocaInst::isStaticAlloca() const {
1027 // Must be constant size.
1028 if (!isa<ConstantInt>(getArraySize())) return false;
Fangrui Songf78650a2018-07-30 19:41:25 +00001029
Chris Lattner8b291e62008-11-26 02:54:17 +00001030 // Must be in the entry block.
1031 const BasicBlock *Parent = getParent();
Reid Kleckner436c42e2014-01-17 23:58:17 +00001032 return Parent == &Parent->getParent()->front() && !isUsedWithInAlloca();
Chris Lattner8b291e62008-11-26 02:54:17 +00001033}
1034
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001035//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001036// LoadInst Implementation
1037//===----------------------------------------------------------------------===//
1038
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001039void LoadInst::AssertOK() {
Duncan Sands19d0b472010-02-16 11:11:14 +00001040 assert(getOperand(0)->getType()->isPointerTy() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001041 "Ptr must have pointer type.");
Eli Friedman59b66882011-08-09 23:02:53 +00001042 assert(!(isAtomic() && getAlignment() == 0) &&
1043 "Alignment required for atomic load");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001044}
1045
Daniel Dunbar4975db62009-07-25 04:41:11 +00001046LoadInst::LoadInst(Value *Ptr, const Twine &Name, Instruction *InsertBef)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001047 : LoadInst(Ptr, Name, /*isVolatile=*/false, InsertBef) {}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001048
Daniel Dunbar4975db62009-07-25 04:41:11 +00001049LoadInst::LoadInst(Value *Ptr, const Twine &Name, BasicBlock *InsertAE)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001050 : LoadInst(Ptr, Name, /*isVolatile=*/false, InsertAE) {}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001051
David Blaikie0c28fd72015-05-20 21:46:30 +00001052LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001053 Instruction *InsertBef)
David Blaikie0c28fd72015-05-20 21:46:30 +00001054 : LoadInst(Ty, Ptr, Name, isVolatile, /*Align=*/0, InsertBef) {}
Eli Friedman59b66882011-08-09 23:02:53 +00001055
1056LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
1057 BasicBlock *InsertAE)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001058 : LoadInst(Ptr, Name, isVolatile, /*Align=*/0, InsertAE) {}
Christopher Lamb84485702007-04-22 19:24:39 +00001059
David Blaikieb7a029872015-04-17 19:56:21 +00001060LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Christopher Lamb84485702007-04-22 19:24:39 +00001061 unsigned Align, Instruction *InsertBef)
JF Bastien800f87a2016-04-06 21:19:33 +00001062 : LoadInst(Ty, Ptr, Name, isVolatile, Align, AtomicOrdering::NotAtomic,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001063 SyncScope::System, InsertBef) {}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001064
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001065LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +00001066 unsigned Align, BasicBlock *InsertAE)
JF Bastien800f87a2016-04-06 21:19:33 +00001067 : LoadInst(Ptr, Name, isVolatile, Align, AtomicOrdering::NotAtomic,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001068 SyncScope::System, InsertAE) {}
Dan Gohman68659282007-07-18 20:51:11 +00001069
David Blaikie15d9a4c2015-04-06 20:59:48 +00001070LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001071 unsigned Align, AtomicOrdering Order,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001072 SyncScope::ID SSID, Instruction *InsertBef)
David Blaikie15d9a4c2015-04-06 20:59:48 +00001073 : UnaryInstruction(Ty, Load, Ptr, InsertBef) {
David Blaikie79009f82015-05-20 20:22:31 +00001074 assert(Ty == cast<PointerType>(Ptr->getType())->getElementType());
Eli Friedman59b66882011-08-09 23:02:53 +00001075 setVolatile(isVolatile);
1076 setAlignment(Align);
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001077 setAtomic(Order, SSID);
Eli Friedman59b66882011-08-09 23:02:53 +00001078 AssertOK();
1079 setName(Name);
1080}
1081
Fangrui Songf78650a2018-07-30 19:41:25 +00001082LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001083 unsigned Align, AtomicOrdering Order,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001084 SyncScope::ID SSID,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001085 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001086 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001087 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +00001088 setVolatile(isVolatile);
Eli Friedman59b66882011-08-09 23:02:53 +00001089 setAlignment(Align);
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001090 setAtomic(Order, SSID);
Chris Lattner0f048162007-02-13 07:54:42 +00001091 AssertOK();
1092 setName(Name);
1093}
1094
Daniel Dunbar27096822009-08-11 18:11:15 +00001095LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
1096 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1097 Load, Ptr, InsertBef) {
1098 setVolatile(false);
1099 setAlignment(0);
JF Bastien800f87a2016-04-06 21:19:33 +00001100 setAtomic(AtomicOrdering::NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001101 AssertOK();
1102 if (Name && Name[0]) setName(Name);
1103}
1104
1105LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
1106 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1107 Load, Ptr, InsertAE) {
1108 setVolatile(false);
1109 setAlignment(0);
JF Bastien800f87a2016-04-06 21:19:33 +00001110 setAtomic(AtomicOrdering::NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001111 AssertOK();
1112 if (Name && Name[0]) setName(Name);
1113}
1114
David Blaikie0c28fd72015-05-20 21:46:30 +00001115LoadInst::LoadInst(Type *Ty, Value *Ptr, const char *Name, bool isVolatile,
Daniel Dunbar27096822009-08-11 18:11:15 +00001116 Instruction *InsertBef)
David Blaikie0c28fd72015-05-20 21:46:30 +00001117 : UnaryInstruction(Ty, Load, Ptr, InsertBef) {
1118 assert(Ty == cast<PointerType>(Ptr->getType())->getElementType());
Daniel Dunbar27096822009-08-11 18:11:15 +00001119 setVolatile(isVolatile);
1120 setAlignment(0);
JF Bastien800f87a2016-04-06 21:19:33 +00001121 setAtomic(AtomicOrdering::NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001122 AssertOK();
1123 if (Name && Name[0]) setName(Name);
1124}
1125
1126LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
1127 BasicBlock *InsertAE)
1128 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1129 Load, Ptr, InsertAE) {
1130 setVolatile(isVolatile);
1131 setAlignment(0);
JF Bastien800f87a2016-04-06 21:19:33 +00001132 setAtomic(AtomicOrdering::NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001133 AssertOK();
1134 if (Name && Name[0]) setName(Name);
1135}
1136
Christopher Lamb84485702007-04-22 19:24:39 +00001137void LoadInst::setAlignment(unsigned Align) {
1138 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001139 assert(Align <= MaximumAlignment &&
1140 "Alignment is greater than MaximumAlignment!");
Eli Friedman59b66882011-08-09 23:02:53 +00001141 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) |
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001142 ((Log2_32(Align)+1)<<1));
Dan Gohmana7e5a242010-07-28 20:12:04 +00001143 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +00001144}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001145
1146//===----------------------------------------------------------------------===//
1147// StoreInst Implementation
1148//===----------------------------------------------------------------------===//
1149
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001150void StoreInst::AssertOK() {
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001151 assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001152 assert(getOperand(1)->getType()->isPointerTy() &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001153 "Ptr must have pointer type!");
1154 assert(getOperand(0)->getType() ==
1155 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +00001156 && "Ptr must be a pointer to Val type!");
Eli Friedman59b66882011-08-09 23:02:53 +00001157 assert(!(isAtomic() && getAlignment() == 0) &&
Mark Lacey1d7c97e2013-12-21 00:00:49 +00001158 "Alignment required for atomic store");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001159}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001160
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001161StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001162 : StoreInst(val, addr, /*isVolatile=*/false, InsertBefore) {}
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001163
1164StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001165 : StoreInst(val, addr, /*isVolatile=*/false, InsertAtEnd) {}
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001166
Misha Brukmanb1c93172005-04-21 23:48:37 +00001167StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001168 Instruction *InsertBefore)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001169 : StoreInst(val, addr, isVolatile, /*Align=*/0, InsertBefore) {}
Christopher Lamb84485702007-04-22 19:24:39 +00001170
1171StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001172 BasicBlock *InsertAtEnd)
1173 : StoreInst(val, addr, isVolatile, /*Align=*/0, InsertAtEnd) {}
1174
1175StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, unsigned Align,
1176 Instruction *InsertBefore)
JF Bastien800f87a2016-04-06 21:19:33 +00001177 : StoreInst(val, addr, isVolatile, Align, AtomicOrdering::NotAtomic,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001178 SyncScope::System, InsertBefore) {}
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001179
1180StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, unsigned Align,
1181 BasicBlock *InsertAtEnd)
JF Bastien800f87a2016-04-06 21:19:33 +00001182 : StoreInst(val, addr, isVolatile, Align, AtomicOrdering::NotAtomic,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001183 SyncScope::System, InsertAtEnd) {}
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001184
Misha Brukmanb1c93172005-04-21 23:48:37 +00001185StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001186 unsigned Align, AtomicOrdering Order,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001187 SyncScope::ID SSID,
Eli Friedman59b66882011-08-09 23:02:53 +00001188 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001189 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001190 OperandTraits<StoreInst>::op_begin(this),
1191 OperandTraits<StoreInst>::operands(this),
Eli Friedman59b66882011-08-09 23:02:53 +00001192 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001193 Op<0>() = val;
1194 Op<1>() = addr;
Dan Gohman68659282007-07-18 20:51:11 +00001195 setVolatile(isVolatile);
1196 setAlignment(Align);
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001197 setAtomic(Order, SSID);
Dan Gohman68659282007-07-18 20:51:11 +00001198 AssertOK();
1199}
1200
1201StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001202 unsigned Align, AtomicOrdering Order,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001203 SyncScope::ID SSID,
Eli Friedman59b66882011-08-09 23:02:53 +00001204 BasicBlock *InsertAtEnd)
1205 : Instruction(Type::getVoidTy(val->getContext()), Store,
1206 OperandTraits<StoreInst>::op_begin(this),
1207 OperandTraits<StoreInst>::operands(this),
1208 InsertAtEnd) {
1209 Op<0>() = val;
1210 Op<1>() = addr;
1211 setVolatile(isVolatile);
1212 setAlignment(Align);
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001213 setAtomic(Order, SSID);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001214 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001215}
1216
Christopher Lamb84485702007-04-22 19:24:39 +00001217void StoreInst::setAlignment(unsigned Align) {
1218 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001219 assert(Align <= MaximumAlignment &&
1220 "Alignment is greater than MaximumAlignment!");
Eli Friedman59b66882011-08-09 23:02:53 +00001221 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) |
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001222 ((Log2_32(Align)+1) << 1));
Dan Gohmana7e5a242010-07-28 20:12:04 +00001223 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +00001224}
1225
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001226//===----------------------------------------------------------------------===//
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001227// AtomicCmpXchgInst Implementation
1228//===----------------------------------------------------------------------===//
1229
1230void AtomicCmpXchgInst::Init(Value *Ptr, Value *Cmp, Value *NewVal,
Tim Northovere94a5182014-03-11 10:48:52 +00001231 AtomicOrdering SuccessOrdering,
1232 AtomicOrdering FailureOrdering,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001233 SyncScope::ID SSID) {
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001234 Op<0>() = Ptr;
1235 Op<1>() = Cmp;
1236 Op<2>() = NewVal;
Tim Northovere94a5182014-03-11 10:48:52 +00001237 setSuccessOrdering(SuccessOrdering);
1238 setFailureOrdering(FailureOrdering);
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001239 setSyncScopeID(SSID);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001240
1241 assert(getOperand(0) && getOperand(1) && getOperand(2) &&
1242 "All operands must be non-null!");
1243 assert(getOperand(0)->getType()->isPointerTy() &&
1244 "Ptr must have pointer type!");
1245 assert(getOperand(1)->getType() ==
1246 cast<PointerType>(getOperand(0)->getType())->getElementType()
1247 && "Ptr must be a pointer to Cmp type!");
1248 assert(getOperand(2)->getType() ==
1249 cast<PointerType>(getOperand(0)->getType())->getElementType()
1250 && "Ptr must be a pointer to NewVal type!");
JF Bastien800f87a2016-04-06 21:19:33 +00001251 assert(SuccessOrdering != AtomicOrdering::NotAtomic &&
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001252 "AtomicCmpXchg instructions must be atomic!");
JF Bastien800f87a2016-04-06 21:19:33 +00001253 assert(FailureOrdering != AtomicOrdering::NotAtomic &&
Tim Northovere94a5182014-03-11 10:48:52 +00001254 "AtomicCmpXchg instructions must be atomic!");
JF Bastien800f87a2016-04-06 21:19:33 +00001255 assert(!isStrongerThan(FailureOrdering, SuccessOrdering) &&
1256 "AtomicCmpXchg failure argument shall be no stronger than the success "
1257 "argument");
1258 assert(FailureOrdering != AtomicOrdering::Release &&
1259 FailureOrdering != AtomicOrdering::AcquireRelease &&
Tim Northovere94a5182014-03-11 10:48:52 +00001260 "AtomicCmpXchg failure ordering cannot include release semantics");
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001261}
1262
1263AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
Tim Northovere94a5182014-03-11 10:48:52 +00001264 AtomicOrdering SuccessOrdering,
1265 AtomicOrdering FailureOrdering,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001266 SyncScope::ID SSID,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001267 Instruction *InsertBefore)
Tim Northover420a2162014-06-13 14:24:07 +00001268 : Instruction(
Serge Gueltone38003f2017-05-09 19:31:13 +00001269 StructType::get(Cmp->getType(), Type::getInt1Ty(Cmp->getContext())),
Tim Northover420a2162014-06-13 14:24:07 +00001270 AtomicCmpXchg, OperandTraits<AtomicCmpXchgInst>::op_begin(this),
1271 OperandTraits<AtomicCmpXchgInst>::operands(this), InsertBefore) {
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001272 Init(Ptr, Cmp, NewVal, SuccessOrdering, FailureOrdering, SSID);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001273}
1274
1275AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
Tim Northovere94a5182014-03-11 10:48:52 +00001276 AtomicOrdering SuccessOrdering,
1277 AtomicOrdering FailureOrdering,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001278 SyncScope::ID SSID,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001279 BasicBlock *InsertAtEnd)
Tim Northover420a2162014-06-13 14:24:07 +00001280 : Instruction(
Serge Gueltone38003f2017-05-09 19:31:13 +00001281 StructType::get(Cmp->getType(), Type::getInt1Ty(Cmp->getContext())),
Tim Northover420a2162014-06-13 14:24:07 +00001282 AtomicCmpXchg, OperandTraits<AtomicCmpXchgInst>::op_begin(this),
1283 OperandTraits<AtomicCmpXchgInst>::operands(this), InsertAtEnd) {
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001284 Init(Ptr, Cmp, NewVal, SuccessOrdering, FailureOrdering, SSID);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001285}
Tim Northover420a2162014-06-13 14:24:07 +00001286
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001287//===----------------------------------------------------------------------===//
1288// AtomicRMWInst Implementation
1289//===----------------------------------------------------------------------===//
1290
1291void AtomicRMWInst::Init(BinOp Operation, Value *Ptr, Value *Val,
1292 AtomicOrdering Ordering,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001293 SyncScope::ID SSID) {
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001294 Op<0>() = Ptr;
1295 Op<1>() = Val;
1296 setOperation(Operation);
1297 setOrdering(Ordering);
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001298 setSyncScopeID(SSID);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001299
1300 assert(getOperand(0) && getOperand(1) &&
1301 "All operands must be non-null!");
1302 assert(getOperand(0)->getType()->isPointerTy() &&
1303 "Ptr must have pointer type!");
1304 assert(getOperand(1)->getType() ==
1305 cast<PointerType>(getOperand(0)->getType())->getElementType()
1306 && "Ptr must be a pointer to Val type!");
JF Bastien800f87a2016-04-06 21:19:33 +00001307 assert(Ordering != AtomicOrdering::NotAtomic &&
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001308 "AtomicRMW instructions must be atomic!");
1309}
1310
1311AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
1312 AtomicOrdering Ordering,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001313 SyncScope::ID SSID,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001314 Instruction *InsertBefore)
1315 : Instruction(Val->getType(), AtomicRMW,
1316 OperandTraits<AtomicRMWInst>::op_begin(this),
1317 OperandTraits<AtomicRMWInst>::operands(this),
1318 InsertBefore) {
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001319 Init(Operation, Ptr, Val, Ordering, SSID);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001320}
1321
1322AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
1323 AtomicOrdering Ordering,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001324 SyncScope::ID SSID,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001325 BasicBlock *InsertAtEnd)
1326 : Instruction(Val->getType(), AtomicRMW,
1327 OperandTraits<AtomicRMWInst>::op_begin(this),
1328 OperandTraits<AtomicRMWInst>::operands(this),
1329 InsertAtEnd) {
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001330 Init(Operation, Ptr, Val, Ordering, SSID);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001331}
1332
Matt Arsenaultb02ba992018-10-02 23:44:11 +00001333StringRef AtomicRMWInst::getOperationName(BinOp Op) {
1334 switch (Op) {
1335 case AtomicRMWInst::Xchg:
1336 return "xchg";
1337 case AtomicRMWInst::Add:
1338 return "add";
1339 case AtomicRMWInst::Sub:
1340 return "sub";
1341 case AtomicRMWInst::And:
1342 return "and";
1343 case AtomicRMWInst::Nand:
1344 return "nand";
1345 case AtomicRMWInst::Or:
1346 return "or";
1347 case AtomicRMWInst::Xor:
1348 return "xor";
1349 case AtomicRMWInst::Max:
1350 return "max";
1351 case AtomicRMWInst::Min:
1352 return "min";
1353 case AtomicRMWInst::UMax:
1354 return "umax";
1355 case AtomicRMWInst::UMin:
1356 return "umin";
1357 case AtomicRMWInst::BAD_BINOP:
1358 return "<invalid operation>";
1359 }
1360
1361 llvm_unreachable("invalid atomicrmw operation");
1362}
1363
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001364//===----------------------------------------------------------------------===//
Eli Friedmanfee02c62011-07-25 23:16:38 +00001365// FenceInst Implementation
1366//===----------------------------------------------------------------------===//
1367
Fangrui Songf78650a2018-07-30 19:41:25 +00001368FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001369 SyncScope::ID SSID,
Eli Friedmanfee02c62011-07-25 23:16:38 +00001370 Instruction *InsertBefore)
Craig Topperc6207612014-04-09 06:08:46 +00001371 : Instruction(Type::getVoidTy(C), Fence, nullptr, 0, InsertBefore) {
Eli Friedmanfee02c62011-07-25 23:16:38 +00001372 setOrdering(Ordering);
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001373 setSyncScopeID(SSID);
Eli Friedmanfee02c62011-07-25 23:16:38 +00001374}
1375
Fangrui Songf78650a2018-07-30 19:41:25 +00001376FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001377 SyncScope::ID SSID,
Eli Friedmanfee02c62011-07-25 23:16:38 +00001378 BasicBlock *InsertAtEnd)
Craig Topperc6207612014-04-09 06:08:46 +00001379 : Instruction(Type::getVoidTy(C), Fence, nullptr, 0, InsertAtEnd) {
Eli Friedmanfee02c62011-07-25 23:16:38 +00001380 setOrdering(Ordering);
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001381 setSyncScopeID(SSID);
Eli Friedmanfee02c62011-07-25 23:16:38 +00001382}
1383
1384//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001385// GetElementPtrInst Implementation
1386//===----------------------------------------------------------------------===//
1387
Jay Foadd1b78492011-07-25 09:48:08 +00001388void GetElementPtrInst::init(Value *Ptr, ArrayRef<Value *> IdxList,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001389 const Twine &Name) {
Pete Cooperb4eede22015-06-12 17:48:10 +00001390 assert(getNumOperands() == 1 + IdxList.size() &&
1391 "NumOperands not initialized?");
Pete Coopereb31b682015-05-21 22:48:54 +00001392 Op<0>() = Ptr;
Fangrui Song75709322018-11-17 01:44:25 +00001393 llvm::copy(IdxList, op_begin() + 1);
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001394 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001395}
1396
Gabor Greiff6caff662008-05-10 08:32:32 +00001397GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
David Blaikie73cf8722015-05-05 18:03:48 +00001398 : Instruction(GEPI.getType(), GetElementPtr,
1399 OperandTraits<GetElementPtrInst>::op_end(this) -
1400 GEPI.getNumOperands(),
1401 GEPI.getNumOperands()),
David Blaikief5147ef2015-06-01 03:09:34 +00001402 SourceElementType(GEPI.SourceElementType),
1403 ResultElementType(GEPI.ResultElementType) {
Jay Foadd1b78492011-07-25 09:48:08 +00001404 std::copy(GEPI.op_begin(), GEPI.op_end(), op_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001405 SubclassOptionalData = GEPI.SubclassOptionalData;
Gabor Greiff6caff662008-05-10 08:32:32 +00001406}
1407
Chris Lattner6090a422009-03-09 04:46:40 +00001408/// getIndexedType - Returns the type of the element that would be accessed with
1409/// a gep instruction with the specified parameters.
1410///
1411/// The Idxs pointer should point to a continuous piece of memory containing the
1412/// indices, either as Value* or uint64_t.
1413///
1414/// A null type is returned if the indices are invalid for the specified
1415/// pointer type.
1416///
Matthijs Kooijman04468622008-07-29 08:46:11 +00001417template <typename IndexTy>
David Blaikied288fb82015-03-30 21:41:43 +00001418static Type *getIndexedTypeInternal(Type *Agg, ArrayRef<IndexTy> IdxList) {
Chris Lattner6090a422009-03-09 04:46:40 +00001419 // Handle the special case of the empty set index set, which is always valid.
Jay Foadd1b78492011-07-25 09:48:08 +00001420 if (IdxList.empty())
Dan Gohman12fce772008-05-15 19:50:34 +00001421 return Agg;
Nadav Rotem3924cb02011-12-05 06:29:09 +00001422
Chris Lattner6090a422009-03-09 04:46:40 +00001423 // If there is at least one index, the top level type must be sized, otherwise
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001424 // it cannot be 'stepped over'.
1425 if (!Agg->isSized())
Craig Topperc6207612014-04-09 06:08:46 +00001426 return nullptr;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001427
Dan Gohman1ecaf452008-05-31 00:58:22 +00001428 unsigned CurIdx = 1;
Jay Foadd1b78492011-07-25 09:48:08 +00001429 for (; CurIdx != IdxList.size(); ++CurIdx) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001430 CompositeType *CT = dyn_cast<CompositeType>(Agg);
Craig Topperc6207612014-04-09 06:08:46 +00001431 if (!CT || CT->isPointerTy()) return nullptr;
Jay Foadd1b78492011-07-25 09:48:08 +00001432 IndexTy Index = IdxList[CurIdx];
Craig Topperc6207612014-04-09 06:08:46 +00001433 if (!CT->indexValid(Index)) return nullptr;
Dan Gohman1ecaf452008-05-31 00:58:22 +00001434 Agg = CT->getTypeAtIndex(Index);
Dan Gohman1ecaf452008-05-31 00:58:22 +00001435 }
Craig Topperc6207612014-04-09 06:08:46 +00001436 return CurIdx == IdxList.size() ? Agg : nullptr;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001437}
1438
David Blaikied288fb82015-03-30 21:41:43 +00001439Type *GetElementPtrInst::getIndexedType(Type *Ty, ArrayRef<Value *> IdxList) {
1440 return getIndexedTypeInternal(Ty, IdxList);
Matthijs Kooijman04468622008-07-29 08:46:11 +00001441}
1442
David Blaikied288fb82015-03-30 21:41:43 +00001443Type *GetElementPtrInst::getIndexedType(Type *Ty,
Jay Foadd1b78492011-07-25 09:48:08 +00001444 ArrayRef<Constant *> IdxList) {
David Blaikied288fb82015-03-30 21:41:43 +00001445 return getIndexedTypeInternal(Ty, IdxList);
Jay Foad1d4a8fe2011-01-14 08:07:43 +00001446}
1447
David Blaikied288fb82015-03-30 21:41:43 +00001448Type *GetElementPtrInst::getIndexedType(Type *Ty, ArrayRef<uint64_t> IdxList) {
1449 return getIndexedTypeInternal(Ty, IdxList);
Matthijs Kooijman04468622008-07-29 08:46:11 +00001450}
1451
Chris Lattner45f15572007-04-14 00:12:57 +00001452/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1453/// zeros. If so, the result pointer and the first operand have the same
1454/// value, just potentially different types.
1455bool GetElementPtrInst::hasAllZeroIndices() const {
1456 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1457 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1458 if (!CI->isZero()) return false;
1459 } else {
1460 return false;
1461 }
1462 }
1463 return true;
1464}
1465
Chris Lattner27058292007-04-27 20:35:56 +00001466/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1467/// constant integers. If so, the result pointer and the first operand have
1468/// a constant offset between them.
1469bool GetElementPtrInst::hasAllConstantIndices() const {
1470 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1471 if (!isa<ConstantInt>(getOperand(i)))
1472 return false;
1473 }
1474 return true;
1475}
1476
Dan Gohman1b849082009-09-07 23:54:19 +00001477void GetElementPtrInst::setIsInBounds(bool B) {
1478 cast<GEPOperator>(this)->setIsInBounds(B);
1479}
Chris Lattner45f15572007-04-14 00:12:57 +00001480
Nick Lewycky28a5f252009-09-27 21:33:04 +00001481bool GetElementPtrInst::isInBounds() const {
1482 return cast<GEPOperator>(this)->isInBounds();
1483}
1484
Chandler Carruth1e140532012-12-11 10:29:10 +00001485bool GetElementPtrInst::accumulateConstantOffset(const DataLayout &DL,
1486 APInt &Offset) const {
Chandler Carruth7ec41c72012-12-11 11:05:15 +00001487 // Delegate to the generic GEPOperator implementation.
1488 return cast<GEPOperator>(this)->accumulateConstantOffset(DL, Offset);
Chandler Carruth1e140532012-12-11 10:29:10 +00001489}
1490
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001491//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001492// ExtractElementInst Implementation
1493//===----------------------------------------------------------------------===//
1494
1495ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001496 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001497 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001498 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001499 ExtractElement,
1500 OperandTraits<ExtractElementInst>::op_begin(this),
1501 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001502 assert(isValidOperands(Val, Index) &&
1503 "Invalid extractelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001504 Op<0>() = Val;
1505 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001506 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001507}
1508
1509ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001510 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001511 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001512 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001513 ExtractElement,
1514 OperandTraits<ExtractElementInst>::op_begin(this),
1515 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001516 assert(isValidOperands(Val, Index) &&
1517 "Invalid extractelement instruction operands!");
1518
Gabor Greif2d3024d2008-05-26 21:33:52 +00001519 Op<0>() = Val;
1520 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001521 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001522}
1523
Chris Lattner54865b32006-04-08 04:05:48 +00001524bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00001525 if (!Val->getType()->isVectorTy() || !Index->getType()->isIntegerTy())
Chris Lattner54865b32006-04-08 04:05:48 +00001526 return false;
1527 return true;
1528}
1529
Robert Bocchino23004482006-01-10 19:05:34 +00001530//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001531// InsertElementInst Implementation
1532//===----------------------------------------------------------------------===//
1533
Chris Lattner54865b32006-04-08 04:05:48 +00001534InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001535 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001536 Instruction *InsertBef)
Gabor Greiff6caff662008-05-10 08:32:32 +00001537 : Instruction(Vec->getType(), InsertElement,
1538 OperandTraits<InsertElementInst>::op_begin(this),
1539 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001540 assert(isValidOperands(Vec, Elt, Index) &&
1541 "Invalid insertelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001542 Op<0>() = Vec;
1543 Op<1>() = Elt;
1544 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001545 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001546}
1547
Chris Lattner54865b32006-04-08 04:05:48 +00001548InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001549 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001550 BasicBlock *InsertAE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001551 : Instruction(Vec->getType(), InsertElement,
1552 OperandTraits<InsertElementInst>::op_begin(this),
1553 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001554 assert(isValidOperands(Vec, Elt, Index) &&
1555 "Invalid insertelement instruction operands!");
1556
Gabor Greif2d3024d2008-05-26 21:33:52 +00001557 Op<0>() = Vec;
1558 Op<1>() = Elt;
1559 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001560 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001561}
1562
Fangrui Songf78650a2018-07-30 19:41:25 +00001563bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
Chris Lattner54865b32006-04-08 04:05:48 +00001564 const Value *Index) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001565 if (!Vec->getType()->isVectorTy())
Reid Spencer09575ba2007-02-15 03:39:18 +00001566 return false; // First operand of insertelement must be vector type.
Fangrui Songf78650a2018-07-30 19:41:25 +00001567
Reid Spencerd84d35b2007-02-15 02:26:10 +00001568 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001569 return false;// Second operand of insertelement must be vector element type.
Fangrui Songf78650a2018-07-30 19:41:25 +00001570
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00001571 if (!Index->getType()->isIntegerTy())
Dan Gohman4fe64de2009-06-14 23:30:43 +00001572 return false; // Third operand of insertelement must be i32.
Chris Lattner54865b32006-04-08 04:05:48 +00001573 return true;
1574}
1575
Robert Bocchinoca27f032006-01-17 20:07:22 +00001576//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001577// ShuffleVectorInst Implementation
1578//===----------------------------------------------------------------------===//
1579
1580ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001581 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001582 Instruction *InsertBefore)
Owen Anderson4056ca92009-07-29 22:17:13 +00001583: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
Mon P Wang25f01062008-11-10 04:46:22 +00001584 cast<VectorType>(Mask->getType())->getNumElements()),
1585 ShuffleVector,
1586 OperandTraits<ShuffleVectorInst>::op_begin(this),
1587 OperandTraits<ShuffleVectorInst>::operands(this),
1588 InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001589 assert(isValidOperands(V1, V2, Mask) &&
1590 "Invalid shuffle vector instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001591 Op<0>() = V1;
1592 Op<1>() = V2;
1593 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001594 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001595}
1596
1597ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001598 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001599 BasicBlock *InsertAtEnd)
Dan Gohmane5af8cd2009-08-25 23:27:45 +00001600: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
1601 cast<VectorType>(Mask->getType())->getNumElements()),
1602 ShuffleVector,
1603 OperandTraits<ShuffleVectorInst>::op_begin(this),
1604 OperandTraits<ShuffleVectorInst>::operands(this),
1605 InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001606 assert(isValidOperands(V1, V2, Mask) &&
1607 "Invalid shuffle vector instruction operands!");
1608
Gabor Greif2d3024d2008-05-26 21:33:52 +00001609 Op<0>() = V1;
1610 Op<1>() = V2;
1611 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001612 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001613}
1614
Mon P Wang25f01062008-11-10 04:46:22 +00001615bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001616 const Value *Mask) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001617 // V1 and V2 must be vectors of the same type.
Duncan Sands19d0b472010-02-16 11:11:14 +00001618 if (!V1->getType()->isVectorTy() || V1->getType() != V2->getType())
Chris Lattnerf724e342008-03-02 05:28:33 +00001619 return false;
Fangrui Songf78650a2018-07-30 19:41:25 +00001620
Chris Lattner1dcb6542012-01-25 23:49:49 +00001621 // Mask must be vector of i32.
Sanjay Patel8bd52282017-04-19 16:22:19 +00001622 auto *MaskTy = dyn_cast<VectorType>(Mask->getType());
Craig Topperc6207612014-04-09 06:08:46 +00001623 if (!MaskTy || !MaskTy->getElementType()->isIntegerTy(32))
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001624 return false;
Nate Begeman60a31c32010-08-13 00:16:46 +00001625
1626 // Check to see if Mask is valid.
Chris Lattner1dcb6542012-01-25 23:49:49 +00001627 if (isa<UndefValue>(Mask) || isa<ConstantAggregateZero>(Mask))
1628 return true;
1629
Sanjay Patel8bd52282017-04-19 16:22:19 +00001630 if (const auto *MV = dyn_cast<ConstantVector>(Mask)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001631 unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements();
Benjamin Kramer3ad5c962014-03-10 15:03:06 +00001632 for (Value *Op : MV->operands()) {
Sanjay Patel8bd52282017-04-19 16:22:19 +00001633 if (auto *CI = dyn_cast<ConstantInt>(Op)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001634 if (CI->uge(V1Size*2))
Nate Begeman60a31c32010-08-13 00:16:46 +00001635 return false;
Benjamin Kramer3ad5c962014-03-10 15:03:06 +00001636 } else if (!isa<UndefValue>(Op)) {
Nate Begeman60a31c32010-08-13 00:16:46 +00001637 return false;
1638 }
1639 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001640 return true;
Mon P Wang6ebf4012011-10-26 00:34:48 +00001641 }
Fangrui Songf78650a2018-07-30 19:41:25 +00001642
Sanjay Patel8bd52282017-04-19 16:22:19 +00001643 if (const auto *CDS = dyn_cast<ConstantDataSequential>(Mask)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001644 unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements();
1645 for (unsigned i = 0, e = MaskTy->getNumElements(); i != e; ++i)
1646 if (CDS->getElementAsInteger(i) >= V1Size*2)
1647 return false;
1648 return true;
1649 }
Fangrui Songf78650a2018-07-30 19:41:25 +00001650
Chris Lattner1dcb6542012-01-25 23:49:49 +00001651 // The bitcode reader can create a place holder for a forward reference
1652 // used as the shuffle mask. When this occurs, the shuffle mask will
1653 // fall into this case and fail. To avoid this error, do this bit of
1654 // ugliness to allow such a mask pass.
Sanjay Patel8bd52282017-04-19 16:22:19 +00001655 if (const auto *CE = dyn_cast<ConstantExpr>(Mask))
Chris Lattner1dcb6542012-01-25 23:49:49 +00001656 if (CE->getOpcode() == Instruction::UserOp1)
1657 return true;
1658
1659 return false;
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001660}
1661
Sanjay Patel2ca33602018-06-19 18:44:00 +00001662int ShuffleVectorInst::getMaskValue(const Constant *Mask, unsigned i) {
Chris Lattnercf129702012-01-26 02:51:13 +00001663 assert(i < Mask->getType()->getVectorNumElements() && "Index out of range");
Sanjay Patel8bd52282017-04-19 16:22:19 +00001664 if (auto *CDS = dyn_cast<ConstantDataSequential>(Mask))
Chris Lattner1dcb6542012-01-25 23:49:49 +00001665 return CDS->getElementAsInteger(i);
Chris Lattnercf129702012-01-26 02:51:13 +00001666 Constant *C = Mask->getAggregateElement(i);
Chris Lattner1dcb6542012-01-25 23:49:49 +00001667 if (isa<UndefValue>(C))
Chris Lattnerf724e342008-03-02 05:28:33 +00001668 return -1;
Chris Lattner1dcb6542012-01-25 23:49:49 +00001669 return cast<ConstantInt>(C)->getZExtValue();
Chris Lattnerf724e342008-03-02 05:28:33 +00001670}
1671
Sanjay Patel2ca33602018-06-19 18:44:00 +00001672void ShuffleVectorInst::getShuffleMask(const Constant *Mask,
Chris Lattnercf129702012-01-26 02:51:13 +00001673 SmallVectorImpl<int> &Result) {
1674 unsigned NumElts = Mask->getType()->getVectorNumElements();
Fangrui Songf78650a2018-07-30 19:41:25 +00001675
Sanjay Patel8bd52282017-04-19 16:22:19 +00001676 if (auto *CDS = dyn_cast<ConstantDataSequential>(Mask)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001677 for (unsigned i = 0; i != NumElts; ++i)
1678 Result.push_back(CDS->getElementAsInteger(i));
1679 return;
Fangrui Songf78650a2018-07-30 19:41:25 +00001680 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001681 for (unsigned i = 0; i != NumElts; ++i) {
1682 Constant *C = Mask->getAggregateElement(i);
1683 Result.push_back(isa<UndefValue>(C) ? -1 :
Chris Lattner3dbad4032012-01-26 00:41:50 +00001684 cast<ConstantInt>(C)->getZExtValue());
Chris Lattner1dcb6542012-01-25 23:49:49 +00001685 }
1686}
1687
Sanjay Patelac619a02018-08-30 15:05:38 +00001688static bool isSingleSourceMaskImpl(ArrayRef<int> Mask, int NumOpElts) {
Sanjay Patel2ca33602018-06-19 18:44:00 +00001689 assert(!Mask.empty() && "Shuffle mask must contain elements");
1690 bool UsesLHS = false;
1691 bool UsesRHS = false;
Sanjay Patelac619a02018-08-30 15:05:38 +00001692 for (int i = 0, NumMaskElts = Mask.size(); i < NumMaskElts; ++i) {
Sanjay Patel2ca33602018-06-19 18:44:00 +00001693 if (Mask[i] == -1)
1694 continue;
Sanjay Patelac619a02018-08-30 15:05:38 +00001695 assert(Mask[i] >= 0 && Mask[i] < (NumOpElts * 2) &&
Sanjay Patel2ca33602018-06-19 18:44:00 +00001696 "Out-of-bounds shuffle mask element");
Sanjay Patelac619a02018-08-30 15:05:38 +00001697 UsesLHS |= (Mask[i] < NumOpElts);
1698 UsesRHS |= (Mask[i] >= NumOpElts);
Sanjay Patel2ca33602018-06-19 18:44:00 +00001699 if (UsesLHS && UsesRHS)
1700 return false;
1701 }
1702 assert((UsesLHS ^ UsesRHS) && "Should have selected from exactly 1 source");
1703 return true;
1704}
1705
Sanjay Patelac619a02018-08-30 15:05:38 +00001706bool ShuffleVectorInst::isSingleSourceMask(ArrayRef<int> Mask) {
1707 // We don't have vector operand size information, so assume operands are the
1708 // same size as the mask.
1709 return isSingleSourceMaskImpl(Mask, Mask.size());
1710}
1711
1712static bool isIdentityMaskImpl(ArrayRef<int> Mask, int NumOpElts) {
1713 if (!isSingleSourceMaskImpl(Mask, NumOpElts))
Sanjay Patel2ca33602018-06-19 18:44:00 +00001714 return false;
Sanjay Patelac619a02018-08-30 15:05:38 +00001715 for (int i = 0, NumMaskElts = Mask.size(); i < NumMaskElts; ++i) {
Sanjay Patel2ca33602018-06-19 18:44:00 +00001716 if (Mask[i] == -1)
1717 continue;
Sanjay Patelac619a02018-08-30 15:05:38 +00001718 if (Mask[i] != i && Mask[i] != (NumOpElts + i))
Sanjay Patel2ca33602018-06-19 18:44:00 +00001719 return false;
1720 }
1721 return true;
1722}
1723
Sanjay Patelac619a02018-08-30 15:05:38 +00001724bool ShuffleVectorInst::isIdentityMask(ArrayRef<int> Mask) {
1725 // We don't have vector operand size information, so assume operands are the
1726 // same size as the mask.
1727 return isIdentityMaskImpl(Mask, Mask.size());
1728}
1729
Sanjay Patel2ca33602018-06-19 18:44:00 +00001730bool ShuffleVectorInst::isReverseMask(ArrayRef<int> Mask) {
1731 if (!isSingleSourceMask(Mask))
1732 return false;
1733 for (int i = 0, NumElts = Mask.size(); i < NumElts; ++i) {
1734 if (Mask[i] == -1)
1735 continue;
1736 if (Mask[i] != (NumElts - 1 - i) && Mask[i] != (NumElts + NumElts - 1 - i))
1737 return false;
1738 }
1739 return true;
1740}
1741
1742bool ShuffleVectorInst::isZeroEltSplatMask(ArrayRef<int> Mask) {
1743 if (!isSingleSourceMask(Mask))
1744 return false;
1745 for (int i = 0, NumElts = Mask.size(); i < NumElts; ++i) {
1746 if (Mask[i] == -1)
1747 continue;
1748 if (Mask[i] != 0 && Mask[i] != NumElts)
1749 return false;
1750 }
1751 return true;
1752}
1753
1754bool ShuffleVectorInst::isSelectMask(ArrayRef<int> Mask) {
1755 // Select is differentiated from identity. It requires using both sources.
1756 if (isSingleSourceMask(Mask))
1757 return false;
1758 for (int i = 0, NumElts = Mask.size(); i < NumElts; ++i) {
1759 if (Mask[i] == -1)
1760 continue;
1761 if (Mask[i] != i && Mask[i] != (NumElts + i))
1762 return false;
1763 }
1764 return true;
1765}
1766
1767bool ShuffleVectorInst::isTransposeMask(ArrayRef<int> Mask) {
1768 // Example masks that will return true:
1769 // v1 = <a, b, c, d>
1770 // v2 = <e, f, g, h>
1771 // trn1 = shufflevector v1, v2 <0, 4, 2, 6> = <a, e, c, g>
1772 // trn2 = shufflevector v1, v2 <1, 5, 3, 7> = <b, f, d, h>
1773
1774 // 1. The number of elements in the mask must be a power-of-2 and at least 2.
1775 int NumElts = Mask.size();
1776 if (NumElts < 2 || !isPowerOf2_32(NumElts))
1777 return false;
1778
1779 // 2. The first element of the mask must be either a 0 or a 1.
1780 if (Mask[0] != 0 && Mask[0] != 1)
1781 return false;
1782
1783 // 3. The difference between the first 2 elements must be equal to the
1784 // number of elements in the mask.
1785 if ((Mask[1] - Mask[0]) != NumElts)
1786 return false;
1787
1788 // 4. The difference between consecutive even-numbered and odd-numbered
1789 // elements must be equal to 2.
1790 for (int i = 2; i < NumElts; ++i) {
1791 int MaskEltVal = Mask[i];
1792 if (MaskEltVal == -1)
1793 return false;
1794 int MaskEltPrevVal = Mask[i - 2];
1795 if (MaskEltVal - MaskEltPrevVal != 2)
1796 return false;
1797 }
1798 return true;
1799}
1800
Simon Pilgrimd0c71602018-11-09 16:28:19 +00001801bool ShuffleVectorInst::isExtractSubvectorMask(ArrayRef<int> Mask,
1802 int NumSrcElts, int &Index) {
1803 // Must extract from a single source.
1804 if (!isSingleSourceMaskImpl(Mask, NumSrcElts))
1805 return false;
1806
1807 // Must be smaller (else this is an Identity shuffle).
Fangrui Song4f2e66c2018-11-09 16:45:37 +00001808 if (NumSrcElts <= (int)Mask.size())
Simon Pilgrimd0c71602018-11-09 16:28:19 +00001809 return false;
1810
1811 // Find start of extraction, accounting that we may start with an UNDEF.
1812 int SubIndex = -1;
1813 for (int i = 0, e = Mask.size(); i != e; ++i) {
1814 int M = Mask[i];
1815 if (M < 0)
1816 continue;
1817 int Offset = (M % NumSrcElts) - i;
1818 if (0 <= SubIndex && SubIndex != Offset)
1819 return false;
1820 SubIndex = Offset;
1821 }
1822
1823 if (0 <= SubIndex) {
1824 Index = SubIndex;
1825 return true;
1826 }
1827 return false;
1828}
1829
Sanjay Patelac619a02018-08-30 15:05:38 +00001830bool ShuffleVectorInst::isIdentityWithPadding() const {
1831 int NumOpElts = Op<0>()->getType()->getVectorNumElements();
1832 int NumMaskElts = getType()->getVectorNumElements();
1833 if (NumMaskElts <= NumOpElts)
1834 return false;
1835
1836 // The first part of the mask must choose elements from exactly 1 source op.
Sanjay Patel8d39ed82018-08-30 16:44:07 +00001837 SmallVector<int, 16> Mask = getShuffleMask();
Sanjay Patelac619a02018-08-30 15:05:38 +00001838 if (!isIdentityMaskImpl(Mask, NumOpElts))
1839 return false;
1840
1841 // All extending must be with undef elements.
1842 for (int i = NumOpElts; i < NumMaskElts; ++i)
1843 if (Mask[i] != -1)
1844 return false;
1845
1846 return true;
1847}
1848
1849bool ShuffleVectorInst::isIdentityWithExtract() const {
1850 int NumOpElts = Op<0>()->getType()->getVectorNumElements();
1851 int NumMaskElts = getType()->getVectorNumElements();
1852 if (NumMaskElts >= NumOpElts)
1853 return false;
1854
1855 return isIdentityMaskImpl(getShuffleMask(), NumOpElts);
1856}
Sanjay Patel2ca33602018-06-19 18:44:00 +00001857
Sanjay Patelfd4976b2018-09-20 15:21:52 +00001858bool ShuffleVectorInst::isConcat() const {
1859 // Vector concatenation is differentiated from identity with padding.
1860 if (isa<UndefValue>(Op<0>()) || isa<UndefValue>(Op<1>()))
1861 return false;
1862
1863 int NumOpElts = Op<0>()->getType()->getVectorNumElements();
1864 int NumMaskElts = getType()->getVectorNumElements();
1865 if (NumMaskElts != NumOpElts * 2)
1866 return false;
1867
1868 // Use the mask length rather than the operands' vector lengths here. We
1869 // already know that the shuffle returns a vector twice as long as the inputs,
1870 // and neither of the inputs are undef vectors. If the mask picks consecutive
1871 // elements from both inputs, then this is a concatenation of the inputs.
1872 return isIdentityMaskImpl(getShuffleMask(), NumMaskElts);
1873}
1874
Dan Gohman12fce772008-05-15 19:50:34 +00001875//===----------------------------------------------------------------------===//
Dan Gohman0752bff2008-05-23 00:36:11 +00001876// InsertValueInst Class
1877//===----------------------------------------------------------------------===//
1878
Fangrui Songf78650a2018-07-30 19:41:25 +00001879void InsertValueInst::init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
Jay Foad57aa6362011-07-13 10:26:04 +00001880 const Twine &Name) {
Pete Cooperb4eede22015-06-12 17:48:10 +00001881 assert(getNumOperands() == 2 && "NumOperands not initialized?");
Jay Foad57aa6362011-07-13 10:26:04 +00001882
1883 // There's no fundamental reason why we require at least one index
1884 // (other than weirdness with &*IdxBegin being invalid; see
1885 // getelementptr's init routine for example). But there's no
1886 // present need to support it.
Eugene Zelenkod761e2c2017-05-15 21:57:41 +00001887 assert(!Idxs.empty() && "InsertValueInst must have at least one index");
Jay Foad57aa6362011-07-13 10:26:04 +00001888
1889 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs) ==
Frits van Bommel16ebe772010-12-05 20:50:26 +00001890 Val->getType() && "Inserted value must match indexed type!");
Dan Gohman1ecaf452008-05-31 00:58:22 +00001891 Op<0>() = Agg;
1892 Op<1>() = Val;
Dan Gohman0752bff2008-05-23 00:36:11 +00001893
Jay Foad57aa6362011-07-13 10:26:04 +00001894 Indices.append(Idxs.begin(), Idxs.end());
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001895 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001896}
1897
1898InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
Gabor Greife9408e62008-05-27 11:03:29 +00001899 : Instruction(IVI.getType(), InsertValue,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001900 OperandTraits<InsertValueInst>::op_begin(this), 2),
1901 Indices(IVI.Indices) {
Dan Gohmand8ca05f2008-06-17 23:25:49 +00001902 Op<0>() = IVI.getOperand(0);
1903 Op<1>() = IVI.getOperand(1);
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001904 SubclassOptionalData = IVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001905}
1906
1907//===----------------------------------------------------------------------===//
Dan Gohman12fce772008-05-15 19:50:34 +00001908// ExtractValueInst Class
1909//===----------------------------------------------------------------------===//
1910
Jay Foad57aa6362011-07-13 10:26:04 +00001911void ExtractValueInst::init(ArrayRef<unsigned> Idxs, const Twine &Name) {
Pete Cooperb4eede22015-06-12 17:48:10 +00001912 assert(getNumOperands() == 1 && "NumOperands not initialized?");
Dan Gohman0752bff2008-05-23 00:36:11 +00001913
Jay Foad57aa6362011-07-13 10:26:04 +00001914 // There's no fundamental reason why we require at least one index.
1915 // But there's no present need to support it.
Eugene Zelenkod761e2c2017-05-15 21:57:41 +00001916 assert(!Idxs.empty() && "ExtractValueInst must have at least one index");
Dan Gohman0752bff2008-05-23 00:36:11 +00001917
Jay Foad57aa6362011-07-13 10:26:04 +00001918 Indices.append(Idxs.begin(), Idxs.end());
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001919 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001920}
1921
1922ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
Gabor Greif21ba1842008-06-06 20:28:12 +00001923 : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)),
Dan Gohman1ecaf452008-05-31 00:58:22 +00001924 Indices(EVI.Indices) {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001925 SubclassOptionalData = EVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001926}
1927
Dan Gohman12fce772008-05-15 19:50:34 +00001928// getIndexedType - Returns the type of the element that would be extracted
1929// with an extractvalue instruction with the specified parameters.
1930//
1931// A null type is returned if the indices are invalid for the specified
1932// pointer type.
1933//
Chris Lattner229907c2011-07-18 04:54:35 +00001934Type *ExtractValueInst::getIndexedType(Type *Agg,
Jay Foad57aa6362011-07-13 10:26:04 +00001935 ArrayRef<unsigned> Idxs) {
Benjamin Kramer3ad5c962014-03-10 15:03:06 +00001936 for (unsigned Index : Idxs) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001937 // We can't use CompositeType::indexValid(Index) here.
1938 // indexValid() always returns true for arrays because getelementptr allows
1939 // out-of-bounds indices. Since we don't allow those for extractvalue and
1940 // insertvalue we need to check array indexing manually.
1941 // Since the only other types we can index into are struct types it's just
1942 // as easy to check those manually as well.
Chris Lattner229907c2011-07-18 04:54:35 +00001943 if (ArrayType *AT = dyn_cast<ArrayType>(Agg)) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001944 if (Index >= AT->getNumElements())
Craig Topperc6207612014-04-09 06:08:46 +00001945 return nullptr;
Chris Lattner229907c2011-07-18 04:54:35 +00001946 } else if (StructType *ST = dyn_cast<StructType>(Agg)) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001947 if (Index >= ST->getNumElements())
Craig Topperc6207612014-04-09 06:08:46 +00001948 return nullptr;
Frits van Bommel16ebe772010-12-05 20:50:26 +00001949 } else {
1950 // Not a valid type to index into.
Craig Topperc6207612014-04-09 06:08:46 +00001951 return nullptr;
Frits van Bommel16ebe772010-12-05 20:50:26 +00001952 }
1953
1954 Agg = cast<CompositeType>(Agg)->getTypeAtIndex(Index);
Dan Gohman12fce772008-05-15 19:50:34 +00001955 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001956 return const_cast<Type*>(Agg);
Dan Gohman12fce772008-05-15 19:50:34 +00001957}
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001958
1959//===----------------------------------------------------------------------===//
Cameron McInallycbde0d92018-11-13 18:15:47 +00001960// UnaryOperator Class
1961//===----------------------------------------------------------------------===//
1962
1963UnaryOperator::UnaryOperator(UnaryOps iType, Value *S,
1964 Type *Ty, const Twine &Name,
1965 Instruction *InsertBefore)
1966 : UnaryInstruction(Ty, iType, S, InsertBefore) {
1967 Op<0>() = S;
1968 setName(Name);
1969 AssertOK();
1970}
1971
1972UnaryOperator::UnaryOperator(UnaryOps iType, Value *S,
1973 Type *Ty, const Twine &Name,
1974 BasicBlock *InsertAtEnd)
1975 : UnaryInstruction(Ty, iType, S, InsertAtEnd) {
1976 Op<0>() = S;
1977 setName(Name);
1978 AssertOK();
1979}
1980
1981UnaryOperator *UnaryOperator::Create(UnaryOps Op, Value *S,
1982 const Twine &Name,
1983 Instruction *InsertBefore) {
1984 return new UnaryOperator(Op, S, S->getType(), Name, InsertBefore);
1985}
1986
1987UnaryOperator *UnaryOperator::Create(UnaryOps Op, Value *S,
1988 const Twine &Name,
1989 BasicBlock *InsertAtEnd) {
1990 UnaryOperator *Res = Create(Op, S, Name);
1991 InsertAtEnd->getInstList().push_back(Res);
1992 return Res;
1993}
1994
1995void UnaryOperator::AssertOK() {
1996 Value *LHS = getOperand(0);
1997 (void)LHS; // Silence warnings.
1998#ifndef NDEBUG
1999 switch (getOpcode()) {
2000 case FNeg:
2001 assert(getType() == LHS->getType() &&
2002 "Unary operation should return same type as operand!");
2003 assert(getType()->isFPOrFPVectorTy() &&
2004 "Tried to create a floating-point operation on a "
2005 "non-floating-point type!");
2006 break;
2007 default: llvm_unreachable("Invalid opcode provided");
2008 }
2009#endif
2010}
2011
2012//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002013// BinaryOperator Class
2014//===----------------------------------------------------------------------===//
2015
Chris Lattner2195fc42007-02-24 00:55:48 +00002016BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Chris Lattner229907c2011-07-18 04:54:35 +00002017 Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00002018 Instruction *InsertBefore)
Dan Gohmana2414ea2010-05-03 22:44:19 +00002019 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00002020 OperandTraits<BinaryOperator>::op_begin(this),
2021 OperandTraits<BinaryOperator>::operands(this),
2022 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002023 Op<0>() = S1;
2024 Op<1>() = S2;
Chris Lattner2195fc42007-02-24 00:55:48 +00002025 setName(Name);
Craig Topper700892f2017-06-26 07:15:59 +00002026 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +00002027}
2028
Fangrui Songf78650a2018-07-30 19:41:25 +00002029BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Chris Lattner229907c2011-07-18 04:54:35 +00002030 Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00002031 BasicBlock *InsertAtEnd)
Dan Gohmana2414ea2010-05-03 22:44:19 +00002032 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00002033 OperandTraits<BinaryOperator>::op_begin(this),
2034 OperandTraits<BinaryOperator>::operands(this),
2035 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002036 Op<0>() = S1;
2037 Op<1>() = S2;
Chris Lattner2195fc42007-02-24 00:55:48 +00002038 setName(Name);
Craig Topper700892f2017-06-26 07:15:59 +00002039 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +00002040}
2041
Craig Topper700892f2017-06-26 07:15:59 +00002042void BinaryOperator::AssertOK() {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002043 Value *LHS = getOperand(0), *RHS = getOperand(1);
Jeffrey Yasskin9b43f332010-12-23 00:58:24 +00002044 (void)LHS; (void)RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002045 assert(LHS->getType() == RHS->getType() &&
2046 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002047#ifndef NDEBUG
Craig Topper700892f2017-06-26 07:15:59 +00002048 switch (getOpcode()) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002049 case Add: case Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00002050 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002051 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002052 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002053 assert(getType()->isIntOrIntVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00002054 "Tried to create an integer operation on a non-integer type!");
2055 break;
2056 case FAdd: case FSub:
2057 case FMul:
2058 assert(getType() == LHS->getType() &&
2059 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002060 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00002061 "Tried to create a floating-point operation on a "
2062 "non-floating-point type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002063 break;
Fangrui Songf78650a2018-07-30 19:41:25 +00002064 case UDiv:
2065 case SDiv:
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002066 assert(getType() == LHS->getType() &&
2067 "Arithmetic operation should return same type as operands!");
Craig Topperd1fbb382017-06-25 17:33:48 +00002068 assert(getType()->isIntOrIntVectorTy() &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002069 "Incorrect operand type (not integer) for S/UDIV");
2070 break;
2071 case FDiv:
2072 assert(getType() == LHS->getType() &&
2073 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002074 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002075 "Incorrect operand type (not floating point) for FDIV");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002076 break;
Fangrui Songf78650a2018-07-30 19:41:25 +00002077 case URem:
2078 case SRem:
Reid Spencer7eb55b32006-11-02 01:53:59 +00002079 assert(getType() == LHS->getType() &&
2080 "Arithmetic operation should return same type as operands!");
Craig Topperd1fbb382017-06-25 17:33:48 +00002081 assert(getType()->isIntOrIntVectorTy() &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00002082 "Incorrect operand type (not integer) for S/UREM");
2083 break;
2084 case FRem:
2085 assert(getType() == LHS->getType() &&
2086 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002087 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002088 "Incorrect operand type (not floating point) for FREM");
Reid Spencer7eb55b32006-11-02 01:53:59 +00002089 break;
Reid Spencer2341c222007-02-02 02:16:23 +00002090 case Shl:
2091 case LShr:
2092 case AShr:
2093 assert(getType() == LHS->getType() &&
2094 "Shift operation should return same type as operands!");
Craig Topperd1fbb382017-06-25 17:33:48 +00002095 assert(getType()->isIntOrIntVectorTy() &&
Nate Begemanfecbc8c2008-07-29 15:49:41 +00002096 "Tried to create a shift operation on a non-integral type!");
Reid Spencer2341c222007-02-02 02:16:23 +00002097 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002098 case And: case Or:
2099 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002100 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002101 "Logical operation should return same type as operands!");
Craig Topperd1fbb382017-06-25 17:33:48 +00002102 assert(getType()->isIntOrIntVectorTy() &&
Misha Brukman3852f652005-01-27 06:46:38 +00002103 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002104 break;
Craig Topper700892f2017-06-26 07:15:59 +00002105 default: llvm_unreachable("Invalid opcode provided");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002106 }
2107#endif
2108}
2109
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002110BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002111 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002112 Instruction *InsertBefore) {
2113 assert(S1->getType() == S2->getType() &&
2114 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00002115 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002116}
2117
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002118BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002119 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002120 BasicBlock *InsertAtEnd) {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002121 BinaryOperator *Res = Create(Op, S1, S2, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002122 InsertAtEnd->getInstList().push_back(Res);
2123 return Res;
2124}
2125
Dan Gohman5476cfd2009-08-12 16:23:25 +00002126BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002127 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002128 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00002129 return new BinaryOperator(Instruction::Sub,
2130 zero, Op,
2131 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002132}
2133
Dan Gohman5476cfd2009-08-12 16:23:25 +00002134BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002135 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002136 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00002137 return new BinaryOperator(Instruction::Sub,
2138 zero, Op,
2139 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002140}
2141
Dan Gohman4ab44202009-12-18 02:58:50 +00002142BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
2143 Instruction *InsertBefore) {
2144 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2145 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertBefore);
2146}
2147
2148BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
2149 BasicBlock *InsertAtEnd) {
2150 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2151 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertAtEnd);
2152}
2153
Duncan Sandsfa5f5962010-02-02 12:53:04 +00002154BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
2155 Instruction *InsertBefore) {
2156 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2157 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertBefore);
2158}
2159
2160BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
2161 BasicBlock *InsertAtEnd) {
2162 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2163 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertAtEnd);
2164}
2165
Dan Gohman5476cfd2009-08-12 16:23:25 +00002166BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00002167 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002168 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Chris Lattner47a86bd2012-01-25 06:02:56 +00002169 return new BinaryOperator(Instruction::FSub, zero, Op,
Dan Gohmana5b96452009-06-04 22:49:04 +00002170 Op->getType(), Name, InsertBefore);
2171}
2172
Dan Gohman5476cfd2009-08-12 16:23:25 +00002173BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00002174 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002175 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Chris Lattner47a86bd2012-01-25 06:02:56 +00002176 return new BinaryOperator(Instruction::FSub, zero, Op,
Dan Gohmana5b96452009-06-04 22:49:04 +00002177 Op->getType(), Name, InsertAtEnd);
2178}
2179
Dan Gohman5476cfd2009-08-12 16:23:25 +00002180BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002181 Instruction *InsertBefore) {
Chris Lattner47a86bd2012-01-25 06:02:56 +00002182 Constant *C = Constant::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00002183 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002184 Op->getType(), Name, InsertBefore);
2185}
2186
Dan Gohman5476cfd2009-08-12 16:23:25 +00002187BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002188 BasicBlock *InsertAtEnd) {
Chris Lattner47a86bd2012-01-25 06:02:56 +00002189 Constant *AllOnes = Constant::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00002190 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002191 Op->getType(), Name, InsertAtEnd);
2192}
2193
Sanjay Patel4ce99d42016-11-16 18:09:44 +00002194// Exchange the two operands to this instruction. This instruction is safe to
2195// use on any binary instruction and does not modify the semantics of the
2196// instruction. If the instruction is order-dependent (SetLT f.e.), the opcode
2197// is changed.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002198bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00002199 if (!isCommutative())
2200 return true; // Can't commute operands
Gabor Greif5ef74042008-05-13 22:51:52 +00002201 Op<0>().swap(Op<1>());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002202 return false;
2203}
2204
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002205//===----------------------------------------------------------------------===//
Duncan Sands05f4df82012-04-16 16:28:59 +00002206// FPMathOperator Class
2207//===----------------------------------------------------------------------===//
2208
Duncan Sands05f4df82012-04-16 16:28:59 +00002209float FPMathOperator::getFPAccuracy() const {
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +00002210 const MDNode *MD =
2211 cast<Instruction>(this)->getMetadata(LLVMContext::MD_fpmath);
Duncan Sands05f4df82012-04-16 16:28:59 +00002212 if (!MD)
2213 return 0.0;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002214 ConstantFP *Accuracy = mdconst::extract<ConstantFP>(MD->getOperand(0));
Duncan Sands9af62982012-04-16 19:39:33 +00002215 return Accuracy->getValueAPF().convertToFloat();
Duncan Sands05f4df82012-04-16 16:28:59 +00002216}
2217
Duncan Sands05f4df82012-04-16 16:28:59 +00002218//===----------------------------------------------------------------------===//
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002219// CastInst Class
2220//===----------------------------------------------------------------------===//
2221
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002222// Just determine if this cast only deals with integral->integral conversion.
2223bool CastInst::isIntegerCast() const {
2224 switch (getOpcode()) {
2225 default: return false;
2226 case Instruction::ZExt:
2227 case Instruction::SExt:
2228 case Instruction::Trunc:
2229 return true;
2230 case Instruction::BitCast:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002231 return getOperand(0)->getType()->isIntegerTy() &&
2232 getType()->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002233 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002234}
2235
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002236bool CastInst::isLosslessCast() const {
2237 // Only BitCast can be lossless, exit fast if we're not BitCast
2238 if (getOpcode() != Instruction::BitCast)
2239 return false;
2240
2241 // Identity cast is always lossless
Ana Pazosb3596022016-02-03 21:34:39 +00002242 Type *SrcTy = getOperand(0)->getType();
2243 Type *DstTy = getType();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002244 if (SrcTy == DstTy)
2245 return true;
Fangrui Songf78650a2018-07-30 19:41:25 +00002246
Reid Spencer8d9336d2006-12-31 05:26:44 +00002247 // Pointer to pointer is always lossless.
Duncan Sands19d0b472010-02-16 11:11:14 +00002248 if (SrcTy->isPointerTy())
2249 return DstTy->isPointerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002250 return false; // Other types have no identity values
2251}
2252
2253/// This function determines if the CastInst does not require any bits to be
2254/// changed in order to effect the cast. Essentially, it identifies cases where
Fangrui Songf78650a2018-07-30 19:41:25 +00002255/// no code gen is necessary for the cast, hence the name no-op cast. For
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002256/// example, the following are all no-op casts:
Dan Gohmane9bc2ba2008-05-12 16:34:30 +00002257/// # bitcast i32* %x to i8*
Fangrui Songf78650a2018-07-30 19:41:25 +00002258/// # bitcast <2 x i32> %x to <4 x i16>
Dan Gohmane9bc2ba2008-05-12 16:34:30 +00002259/// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
Adrian Prantl4dfcc4a2018-05-01 16:10:38 +00002260/// Determine if the described cast is a no-op.
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002261bool CastInst::isNoopCast(Instruction::CastOps Opcode,
Chris Lattner229907c2011-07-18 04:54:35 +00002262 Type *SrcTy,
2263 Type *DestTy,
Mikael Holmen0ec1d252017-10-05 07:07:09 +00002264 const DataLayout &DL) {
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002265 switch (Opcode) {
Craig Topperc514b542012-02-05 22:14:15 +00002266 default: llvm_unreachable("Invalid CastOp");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002267 case Instruction::Trunc:
2268 case Instruction::ZExt:
Fangrui Songf78650a2018-07-30 19:41:25 +00002269 case Instruction::SExt:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002270 case Instruction::FPTrunc:
2271 case Instruction::FPExt:
2272 case Instruction::UIToFP:
2273 case Instruction::SIToFP:
2274 case Instruction::FPToUI:
2275 case Instruction::FPToSI:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002276 case Instruction::AddrSpaceCast:
2277 // TODO: Target informations may give a more accurate answer here.
2278 return false;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002279 case Instruction::BitCast:
2280 return true; // BitCast never modifies bits.
2281 case Instruction::PtrToInt:
Mikael Holmen0ec1d252017-10-05 07:07:09 +00002282 return DL.getIntPtrType(SrcTy)->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002283 DestTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002284 case Instruction::IntToPtr:
Mikael Holmen0ec1d252017-10-05 07:07:09 +00002285 return DL.getIntPtrType(DestTy)->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002286 SrcTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002287 }
2288}
2289
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002290bool CastInst::isNoopCast(const DataLayout &DL) const {
Mikael Holmen6efe5072017-10-03 06:03:49 +00002291 return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), DL);
Matt Arsenaulta236ea52014-03-06 17:33:55 +00002292}
2293
2294/// This function determines if a pair of casts can be eliminated and what
2295/// opcode should be used in the elimination. This assumes that there are two
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002296/// instructions like this:
2297/// * %F = firstOpcode SrcTy %x to MidTy
2298/// * %S = secondOpcode MidTy %F to DstTy
2299/// The function returns a resultOpcode so these two casts can be replaced with:
2300/// * %Replacement = resultOpcode %SrcTy %x to DstTy
Sanjay Patel4dad27e2015-12-11 18:12:01 +00002301/// If no such cast is permitted, the function returns 0.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002302unsigned CastInst::isEliminableCastPair(
2303 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
Duncan Sandse2395dc2012-10-30 16:03:32 +00002304 Type *SrcTy, Type *MidTy, Type *DstTy, Type *SrcIntPtrTy, Type *MidIntPtrTy,
2305 Type *DstIntPtrTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002306 // Define the 144 possibilities for these two cast instructions. The values
2307 // in this matrix determine what to do in a given situation and select the
Fangrui Songf78650a2018-07-30 19:41:25 +00002308 // case in the switch below. The rows correspond to firstOp, the columns
Sanjay Patel4dad27e2015-12-11 18:12:01 +00002309 // correspond to secondOp. In looking at the table below, keep in mind
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002310 // the following cast properties:
2311 //
2312 // Size Compare Source Destination
2313 // Operator Src ? Size Type Sign Type Sign
2314 // -------- ------------ ------------------- ---------------------
2315 // TRUNC > Integer Any Integral Any
2316 // ZEXT < Integral Unsigned Integer Any
2317 // SEXT < Integral Signed Integer Any
2318 // FPTOUI n/a FloatPt n/a Integral Unsigned
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002319 // FPTOSI n/a FloatPt n/a Integral Signed
2320 // UITOFP n/a Integral Unsigned FloatPt n/a
2321 // SITOFP n/a Integral Signed FloatPt n/a
2322 // FPTRUNC > FloatPt n/a FloatPt n/a
2323 // FPEXT < FloatPt n/a FloatPt n/a
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002324 // PTRTOINT n/a Pointer n/a Integral Unsigned
2325 // INTTOPTR n/a Integral Unsigned Pointer n/a
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002326 // BITCAST = FirstClass n/a FirstClass n/a
2327 // ADDRSPCST n/a Pointer n/a Pointer n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00002328 //
2329 // NOTE: some transforms are safe, but we consider them to be non-profitable.
Dan Gohman4fe64de2009-06-14 23:30:43 +00002330 // For example, we could merge "fptoui double to i32" + "zext i32 to i64",
2331 // into "fptoui double to i64", but this loses information about the range
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002332 // of the produced value (we no longer know the top-part is all zeros).
Chris Lattner6f6b4972006-12-05 23:43:59 +00002333 // Further this conversion is often much more expensive for typical hardware,
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002334 // and causes issues when building libgcc. We disallow fptosi+sext for the
Chris Lattner6f6b4972006-12-05 23:43:59 +00002335 // same reason.
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002336 const unsigned numCastOps =
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002337 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
2338 static const uint8_t CastResults[numCastOps][numCastOps] = {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002339 // T F F U S F F P I B A -+
2340 // R Z S P P I I T P 2 N T S |
2341 // U E E 2 2 2 2 R E I T C C +- secondOp
2342 // N X X U S F F N X N 2 V V |
2343 // C T T I I P P C T T P T T -+
2344 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // Trunc -+
Fiona Glaser0d41db12015-04-21 00:05:41 +00002345 { 8, 1, 9,99,99, 2,17,99,99,99, 2, 3, 0}, // ZExt |
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002346 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3, 0}, // SExt |
2347 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // FPToUI |
2348 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // FPToSI |
2349 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // UIToFP +- firstOp
2350 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // SIToFP |
Ahmed Bougacha0ea9d1e2015-05-29 00:04:30 +00002351 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // FPTrunc |
Craig Topper18799f42018-03-02 18:16:51 +00002352 { 99,99,99, 2, 2,99,99, 8, 2,99,99, 4, 0}, // FPExt |
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002353 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3, 0}, // PtrToInt |
2354 { 99,99,99,99,99,99,99,99,99,11,99,15, 0}, // IntToPtr |
2355 { 5, 5, 5, 6, 6, 5, 5, 6, 6,16, 5, 1,14}, // BitCast |
2356 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,13,12}, // AddrSpaceCast -+
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002357 };
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002358
Sanjay Patel93f55dd2015-12-12 00:33:36 +00002359 // TODO: This logic could be encoded into the table above and handled in the
2360 // switch below.
Chris Lattner25eea4d2010-07-12 01:19:22 +00002361 // If either of the casts are a bitcast from scalar to vector, disallow the
Sanjay Patel93f55dd2015-12-12 00:33:36 +00002362 // merging. However, any pair of bitcasts are allowed.
2363 bool IsFirstBitcast = (firstOp == Instruction::BitCast);
2364 bool IsSecondBitcast = (secondOp == Instruction::BitCast);
2365 bool AreBothBitcasts = IsFirstBitcast && IsSecondBitcast;
Nadav Rotem5fc81ff2011-08-29 19:58:36 +00002366
Sanjay Patel93f55dd2015-12-12 00:33:36 +00002367 // Check if any of the casts convert scalars <-> vectors.
2368 if ((IsFirstBitcast && isa<VectorType>(SrcTy) != isa<VectorType>(MidTy)) ||
2369 (IsSecondBitcast && isa<VectorType>(MidTy) != isa<VectorType>(DstTy)))
2370 if (!AreBothBitcasts)
2371 return 0;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002372
2373 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
2374 [secondOp-Instruction::CastOpsBegin];
2375 switch (ElimCase) {
Fangrui Songf78650a2018-07-30 19:41:25 +00002376 case 0:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002377 // Categorically disallowed.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002378 return 0;
Fangrui Songf78650a2018-07-30 19:41:25 +00002379 case 1:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002380 // Allowed, use first cast's opcode.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002381 return firstOp;
Fangrui Songf78650a2018-07-30 19:41:25 +00002382 case 2:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002383 // Allowed, use second cast's opcode.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002384 return secondOp;
Fangrui Songf78650a2018-07-30 19:41:25 +00002385 case 3:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002386 // No-op cast in second op implies firstOp as long as the DestTy
Mon P Wange04b4562010-01-23 04:35:57 +00002387 // is integer and we are not converting between a vector and a
Alp Tokerf907b892013-12-05 05:44:44 +00002388 // non-vector type.
Duncan Sands19d0b472010-02-16 11:11:14 +00002389 if (!SrcTy->isVectorTy() && DstTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002390 return firstOp;
2391 return 0;
2392 case 4:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002393 // No-op cast in second op implies firstOp as long as the DestTy
Chris Lattner531732b2010-01-23 04:42:42 +00002394 // is floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002395 if (DstTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002396 return firstOp;
2397 return 0;
Fangrui Songf78650a2018-07-30 19:41:25 +00002398 case 5:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002399 // No-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002400 // is an integer.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002401 if (SrcTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002402 return secondOp;
2403 return 0;
2404 case 6:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002405 // No-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002406 // is a floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002407 if (SrcTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002408 return secondOp;
2409 return 0;
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002410 case 7: {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002411 // Cannot simplify if address spaces are different!
2412 if (SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace())
2413 return 0;
2414
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002415 unsigned MidSize = MidTy->getScalarSizeInBits();
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002416 // We can still fold this without knowing the actual sizes as long we
2417 // know that the intermediate pointer is the largest possible
2418 // pointer size.
2419 // FIXME: Is this always true?
2420 if (MidSize == 64)
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002421 return Instruction::BitCast;
2422
2423 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size.
Duncan Sandse2395dc2012-10-30 16:03:32 +00002424 if (!SrcIntPtrTy || DstIntPtrTy != SrcIntPtrTy)
Dan Gohman9413de12009-07-21 23:19:40 +00002425 return 0;
Duncan Sandse2395dc2012-10-30 16:03:32 +00002426 unsigned PtrSize = SrcIntPtrTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002427 if (MidSize >= PtrSize)
2428 return Instruction::BitCast;
2429 return 0;
2430 }
2431 case 8: {
2432 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
2433 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
2434 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002435 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2436 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002437 if (SrcSize == DstSize)
2438 return Instruction::BitCast;
2439 else if (SrcSize < DstSize)
2440 return firstOp;
2441 return secondOp;
2442 }
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002443 case 9:
2444 // zext, sext -> zext, because sext can't sign extend after zext
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002445 return Instruction::ZExt;
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002446 case 11: {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002447 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
Duncan Sandse2395dc2012-10-30 16:03:32 +00002448 if (!MidIntPtrTy)
Dan Gohman9413de12009-07-21 23:19:40 +00002449 return 0;
Duncan Sandse2395dc2012-10-30 16:03:32 +00002450 unsigned PtrSize = MidIntPtrTy->getScalarSizeInBits();
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002451 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2452 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002453 if (SrcSize <= PtrSize && SrcSize == DstSize)
2454 return Instruction::BitCast;
2455 return 0;
2456 }
Eugene Zelenkod761e2c2017-05-15 21:57:41 +00002457 case 12:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002458 // addrspacecast, addrspacecast -> bitcast, if SrcAS == DstAS
2459 // addrspacecast, addrspacecast -> addrspacecast, if SrcAS != DstAS
2460 if (SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace())
2461 return Instruction::AddrSpaceCast;
2462 return Instruction::BitCast;
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002463 case 13:
2464 // FIXME: this state can be merged with (1), but the following assert
2465 // is useful to check the correcteness of the sequence due to semantic
2466 // change of bitcast.
2467 assert(
2468 SrcTy->isPtrOrPtrVectorTy() &&
2469 MidTy->isPtrOrPtrVectorTy() &&
2470 DstTy->isPtrOrPtrVectorTy() &&
2471 SrcTy->getPointerAddressSpace() != MidTy->getPointerAddressSpace() &&
2472 MidTy->getPointerAddressSpace() == DstTy->getPointerAddressSpace() &&
2473 "Illegal addrspacecast, bitcast sequence!");
2474 // Allowed, use first cast's opcode
2475 return firstOp;
2476 case 14:
Jingyue Wu77145d92014-06-06 21:52:55 +00002477 // bitcast, addrspacecast -> addrspacecast if the element type of
2478 // bitcast's source is the same as that of addrspacecast's destination.
Peter Collingbourne9d5fd4d2016-11-13 06:58:45 +00002479 if (SrcTy->getScalarType()->getPointerElementType() ==
2480 DstTy->getScalarType()->getPointerElementType())
Jingyue Wu77145d92014-06-06 21:52:55 +00002481 return Instruction::AddrSpaceCast;
2482 return 0;
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002483 case 15:
2484 // FIXME: this state can be merged with (1), but the following assert
2485 // is useful to check the correcteness of the sequence due to semantic
2486 // change of bitcast.
2487 assert(
2488 SrcTy->isIntOrIntVectorTy() &&
2489 MidTy->isPtrOrPtrVectorTy() &&
2490 DstTy->isPtrOrPtrVectorTy() &&
2491 MidTy->getPointerAddressSpace() == DstTy->getPointerAddressSpace() &&
2492 "Illegal inttoptr, bitcast sequence!");
2493 // Allowed, use first cast's opcode
2494 return firstOp;
2495 case 16:
2496 // FIXME: this state can be merged with (2), but the following assert
2497 // is useful to check the correcteness of the sequence due to semantic
2498 // change of bitcast.
2499 assert(
2500 SrcTy->isPtrOrPtrVectorTy() &&
2501 MidTy->isPtrOrPtrVectorTy() &&
2502 DstTy->isIntOrIntVectorTy() &&
2503 SrcTy->getPointerAddressSpace() == MidTy->getPointerAddressSpace() &&
2504 "Illegal bitcast, ptrtoint sequence!");
2505 // Allowed, use second cast's opcode
2506 return secondOp;
Fiona Glaser0d41db12015-04-21 00:05:41 +00002507 case 17:
2508 // (sitofp (zext x)) -> (uitofp x)
2509 return Instruction::UIToFP;
Fangrui Songf78650a2018-07-30 19:41:25 +00002510 case 99:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002511 // Cast combination can't happen (error in input). This is for all cases
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002512 // where the MidTy is not the same for the two cast instructions.
Craig Topperc514b542012-02-05 22:14:15 +00002513 llvm_unreachable("Invalid Cast Combination");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002514 default:
Craig Topperc514b542012-02-05 22:14:15 +00002515 llvm_unreachable("Error in CastResults table!!!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002516 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002517}
2518
Fangrui Songf78650a2018-07-30 19:41:25 +00002519CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002520 const Twine &Name, Instruction *InsertBefore) {
Duncan Sands7f646562011-05-18 09:21:57 +00002521 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002522 // Construct and return the appropriate CastInst subclass
2523 switch (op) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002524 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
2525 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
2526 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
2527 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
2528 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
2529 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
2530 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
2531 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
2532 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
2533 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
2534 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
2535 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
2536 case AddrSpaceCast: return new AddrSpaceCastInst (S, Ty, Name, InsertBefore);
2537 default: llvm_unreachable("Invalid opcode provided");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002538 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002539}
2540
Chris Lattner229907c2011-07-18 04:54:35 +00002541CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002542 const Twine &Name, BasicBlock *InsertAtEnd) {
Duncan Sands7f646562011-05-18 09:21:57 +00002543 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002544 // Construct and return the appropriate CastInst subclass
2545 switch (op) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002546 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
2547 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
2548 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
2549 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
2550 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
2551 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
2552 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
2553 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
2554 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
2555 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
2556 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
2557 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
2558 case AddrSpaceCast: return new AddrSpaceCastInst (S, Ty, Name, InsertAtEnd);
2559 default: llvm_unreachable("Invalid opcode provided");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002560 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002561}
2562
Fangrui Songf78650a2018-07-30 19:41:25 +00002563CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002564 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002565 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002566 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002567 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2568 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002569}
2570
Fangrui Songf78650a2018-07-30 19:41:25 +00002571CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002572 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002573 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002574 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002575 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2576 return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002577}
2578
Fangrui Songf78650a2018-07-30 19:41:25 +00002579CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002580 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002581 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002582 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002583 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2584 return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002585}
2586
Fangrui Songf78650a2018-07-30 19:41:25 +00002587CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002588 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002589 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002590 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002591 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2592 return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002593}
2594
Chris Lattner229907c2011-07-18 04:54:35 +00002595CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002596 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002597 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002598 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002599 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2600 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002601}
2602
Chris Lattner229907c2011-07-18 04:54:35 +00002603CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
Fangrui Songf78650a2018-07-30 19:41:25 +00002604 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002605 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002606 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002607 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2608 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002609}
2610
Chris Lattner229907c2011-07-18 04:54:35 +00002611CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002612 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002613 BasicBlock *InsertAtEnd) {
Matt Arsenault065ced92013-07-31 00:17:33 +00002614 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2615 assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) &&
2616 "Invalid cast");
2617 assert(Ty->isVectorTy() == S->getType()->isVectorTy() && "Invalid cast");
Richard Trieu8dc43232013-07-31 04:07:28 +00002618 assert((!Ty->isVectorTy() ||
2619 Ty->getVectorNumElements() == S->getType()->getVectorNumElements()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002620 "Invalid cast");
2621
Matt Arsenault065ced92013-07-31 00:17:33 +00002622 if (Ty->isIntOrIntVectorTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002623 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002624
Matt Arsenault740980e2014-07-14 17:24:35 +00002625 return CreatePointerBitCastOrAddrSpaceCast(S, Ty, Name, InsertAtEnd);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002626}
2627
Adrian Prantl4dfcc4a2018-05-01 16:10:38 +00002628/// Create a BitCast or a PtrToInt cast instruction
Matt Arsenault065ced92013-07-31 00:17:33 +00002629CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
2630 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002631 Instruction *InsertBefore) {
Evgeniy Stepanovc9bd35b2013-01-15 16:43:00 +00002632 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2633 assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002634 "Invalid cast");
Matt Arsenault065ced92013-07-31 00:17:33 +00002635 assert(Ty->isVectorTy() == S->getType()->isVectorTy() && "Invalid cast");
Richard Trieu8dc43232013-07-31 04:07:28 +00002636 assert((!Ty->isVectorTy() ||
2637 Ty->getVectorNumElements() == S->getType()->getVectorNumElements()) &&
Matt Arsenault065ced92013-07-31 00:17:33 +00002638 "Invalid cast");
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002639
Evgeniy Stepanovc9bd35b2013-01-15 16:43:00 +00002640 if (Ty->isIntOrIntVectorTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002641 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002642
Matt Arsenault740980e2014-07-14 17:24:35 +00002643 return CreatePointerBitCastOrAddrSpaceCast(S, Ty, Name, InsertBefore);
2644}
2645
2646CastInst *CastInst::CreatePointerBitCastOrAddrSpaceCast(
2647 Value *S, Type *Ty,
2648 const Twine &Name,
2649 BasicBlock *InsertAtEnd) {
2650 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2651 assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast");
2652
2653 if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace())
2654 return Create(Instruction::AddrSpaceCast, S, Ty, Name, InsertAtEnd);
2655
2656 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2657}
2658
2659CastInst *CastInst::CreatePointerBitCastOrAddrSpaceCast(
2660 Value *S, Type *Ty,
2661 const Twine &Name,
2662 Instruction *InsertBefore) {
2663 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2664 assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast");
2665
2666 if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace())
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002667 return Create(Instruction::AddrSpaceCast, S, Ty, Name, InsertBefore);
2668
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002669 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002670}
2671
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002672CastInst *CastInst::CreateBitOrPointerCast(Value *S, Type *Ty,
2673 const Twine &Name,
2674 Instruction *InsertBefore) {
2675 if (S->getType()->isPointerTy() && Ty->isIntegerTy())
2676 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
2677 if (S->getType()->isIntegerTy() && Ty->isPointerTy())
2678 return Create(Instruction::IntToPtr, S, Ty, Name, InsertBefore);
2679
2680 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2681}
2682
Matt Arsenault740980e2014-07-14 17:24:35 +00002683CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002684 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002685 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002686 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Chris Lattner5370ae72010-01-10 20:21:42 +00002687 "Invalid integer cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002688 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2689 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002690 Instruction::CastOps opcode =
2691 (SrcBits == DstBits ? Instruction::BitCast :
2692 (SrcBits > DstBits ? Instruction::Trunc :
2693 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002694 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002695}
2696
Fangrui Songf78650a2018-07-30 19:41:25 +00002697CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002698 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002699 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002700 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002701 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002702 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2703 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002704 Instruction::CastOps opcode =
2705 (SrcBits == DstBits ? Instruction::BitCast :
2706 (SrcBits > DstBits ? Instruction::Trunc :
2707 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002708 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002709}
2710
Fangrui Songf78650a2018-07-30 19:41:25 +00002711CastInst *CastInst::CreateFPCast(Value *C, Type *Ty,
2712 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002713 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002714 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002715 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002716 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2717 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002718 Instruction::CastOps opcode =
2719 (SrcBits == DstBits ? Instruction::BitCast :
2720 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002721 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002722}
2723
Fangrui Songf78650a2018-07-30 19:41:25 +00002724CastInst *CastInst::CreateFPCast(Value *C, Type *Ty,
2725 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002726 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002727 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002728 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002729 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2730 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002731 Instruction::CastOps opcode =
2732 (SrcBits == DstBits ? Instruction::BitCast :
2733 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002734 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002735}
2736
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002737// Check whether it is valid to call getCastOpcode for these types.
2738// This routine must be kept in sync with getCastOpcode.
2739bool CastInst::isCastable(Type *SrcTy, Type *DestTy) {
2740 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2741 return false;
2742
2743 if (SrcTy == DestTy)
2744 return true;
2745
2746 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
2747 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
2748 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2749 // An element by element cast. Valid if casting the elements is valid.
2750 SrcTy = SrcVecTy->getElementType();
2751 DestTy = DestVecTy->getElementType();
2752 }
2753
2754 // Get the bit sizes, we'll need these
2755 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2756 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
2757
2758 // Run through the possibilities ...
2759 if (DestTy->isIntegerTy()) { // Casting to integral
David Blaikie9965c5a2015-03-23 19:51:23 +00002760 if (SrcTy->isIntegerTy()) // Casting from integral
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002761 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002762 if (SrcTy->isFloatingPointTy()) // Casting from floating pt
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002763 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002764 if (SrcTy->isVectorTy()) // Casting from vector
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002765 return DestBits == SrcBits;
David Blaikie9965c5a2015-03-23 19:51:23 +00002766 // Casting from something else
2767 return SrcTy->isPointerTy();
Fangrui Songf78650a2018-07-30 19:41:25 +00002768 }
David Blaikie9965c5a2015-03-23 19:51:23 +00002769 if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2770 if (SrcTy->isIntegerTy()) // Casting from integral
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002771 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002772 if (SrcTy->isFloatingPointTy()) // Casting from floating pt
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002773 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002774 if (SrcTy->isVectorTy()) // Casting from vector
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002775 return DestBits == SrcBits;
David Blaikie9965c5a2015-03-23 19:51:23 +00002776 // Casting from something else
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002777 return false;
2778 }
David Blaikie9965c5a2015-03-23 19:51:23 +00002779 if (DestTy->isVectorTy()) // Casting to vector
2780 return DestBits == SrcBits;
2781 if (DestTy->isPointerTy()) { // Casting to pointer
2782 if (SrcTy->isPointerTy()) // Casting from pointer
2783 return true;
2784 return SrcTy->isIntegerTy(); // Casting from integral
Fangrui Songf78650a2018-07-30 19:41:25 +00002785 }
David Blaikie9965c5a2015-03-23 19:51:23 +00002786 if (DestTy->isX86_MMXTy()) {
2787 if (SrcTy->isVectorTy())
2788 return DestBits == SrcBits; // 64-bit vector to MMX
2789 return false;
2790 } // Casting to something else
2791 return false;
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002792}
2793
Matt Arsenaultcacbb232013-07-30 20:45:05 +00002794bool CastInst::isBitCastable(Type *SrcTy, Type *DestTy) {
2795 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2796 return false;
2797
2798 if (SrcTy == DestTy)
2799 return true;
2800
2801 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) {
2802 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy)) {
2803 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2804 // An element by element cast. Valid if casting the elements is valid.
2805 SrcTy = SrcVecTy->getElementType();
2806 DestTy = DestVecTy->getElementType();
2807 }
2808 }
2809 }
2810
2811 if (PointerType *DestPtrTy = dyn_cast<PointerType>(DestTy)) {
2812 if (PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy)) {
2813 return SrcPtrTy->getAddressSpace() == DestPtrTy->getAddressSpace();
2814 }
2815 }
2816
2817 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2818 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
2819
2820 // Could still have vectors of pointers if the number of elements doesn't
2821 // match
2822 if (SrcBits == 0 || DestBits == 0)
2823 return false;
2824
2825 if (SrcBits != DestBits)
2826 return false;
2827
2828 if (DestTy->isX86_MMXTy() || SrcTy->isX86_MMXTy())
2829 return false;
2830
2831 return true;
2832}
2833
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002834bool CastInst::isBitOrNoopPointerCastable(Type *SrcTy, Type *DestTy,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002835 const DataLayout &DL) {
Yichao Yu92c11ee2017-10-22 20:28:17 +00002836 // ptrtoint and inttoptr are not allowed on non-integral pointers
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002837 if (auto *PtrTy = dyn_cast<PointerType>(SrcTy))
2838 if (auto *IntTy = dyn_cast<IntegerType>(DestTy))
Yichao Yu92c11ee2017-10-22 20:28:17 +00002839 return (IntTy->getBitWidth() == DL.getPointerTypeSizeInBits(PtrTy) &&
2840 !DL.isNonIntegralPointerType(PtrTy));
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002841 if (auto *PtrTy = dyn_cast<PointerType>(DestTy))
2842 if (auto *IntTy = dyn_cast<IntegerType>(SrcTy))
Yichao Yu92c11ee2017-10-22 20:28:17 +00002843 return (IntTy->getBitWidth() == DL.getPointerTypeSizeInBits(PtrTy) &&
2844 !DL.isNonIntegralPointerType(PtrTy));
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002845
2846 return isBitCastable(SrcTy, DestTy);
2847}
2848
Matt Arsenaultcacbb232013-07-30 20:45:05 +00002849// Provide a way to get a "cast" where the cast opcode is inferred from the
2850// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002851// logic in the castIsValid function below. This axiom should hold:
2852// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2853// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002854// casting opcode for the arguments passed to it.
Duncan Sands55e50902008-01-06 10:12:28 +00002855// This routine must be kept in sync with isCastable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002856Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00002857CastInst::getCastOpcode(
Chris Lattner229907c2011-07-18 04:54:35 +00002858 const Value *Src, bool SrcIsSigned, Type *DestTy, bool DestIsSigned) {
2859 Type *SrcTy = Src->getType();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002860
Duncan Sands55e50902008-01-06 10:12:28 +00002861 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
2862 "Only first class types are castable!");
2863
Duncan Sandsa8514532011-05-18 07:13:41 +00002864 if (SrcTy == DestTy)
2865 return BitCast;
2866
Matt Arsenaultcacbb232013-07-30 20:45:05 +00002867 // FIXME: Check address space sizes here
Chris Lattner229907c2011-07-18 04:54:35 +00002868 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
2869 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
Duncan Sandsa8514532011-05-18 07:13:41 +00002870 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2871 // An element by element cast. Find the appropriate opcode based on the
2872 // element types.
2873 SrcTy = SrcVecTy->getElementType();
2874 DestTy = DestVecTy->getElementType();
2875 }
2876
2877 // Get the bit sizes, we'll need these
Duncan Sands7f646562011-05-18 09:21:57 +00002878 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2879 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
Duncan Sandsa8514532011-05-18 07:13:41 +00002880
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002881 // Run through the possibilities ...
Duncan Sands9dff9be2010-02-15 16:12:20 +00002882 if (DestTy->isIntegerTy()) { // Casting to integral
2883 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002884 if (DestBits < SrcBits)
2885 return Trunc; // int -> smaller int
2886 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00002887 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002888 return SExt; // signed -> SEXT
2889 else
2890 return ZExt; // unsigned -> ZEXT
2891 } else {
2892 return BitCast; // Same size, No-op cast
2893 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002894 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Fangrui Songf78650a2018-07-30 19:41:25 +00002895 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002896 return FPToSI; // FP -> sint
2897 else
Fangrui Songf78650a2018-07-30 19:41:25 +00002898 return FPToUI; // FP -> uint
Duncan Sands27bd0df2011-05-18 10:59:25 +00002899 } else if (SrcTy->isVectorTy()) {
2900 assert(DestBits == SrcBits &&
2901 "Casting vector to integer of different width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002902 return BitCast; // Same size, no-op cast
2903 } else {
Duncan Sands19d0b472010-02-16 11:11:14 +00002904 assert(SrcTy->isPointerTy() &&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002905 "Casting from a value that is not first-class type");
2906 return PtrToInt; // ptr -> int
2907 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002908 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2909 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00002910 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002911 return SIToFP; // sint -> FP
2912 else
2913 return UIToFP; // uint -> FP
Duncan Sands9dff9be2010-02-15 16:12:20 +00002914 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002915 if (DestBits < SrcBits) {
2916 return FPTrunc; // FP -> smaller FP
2917 } else if (DestBits > SrcBits) {
2918 return FPExt; // FP -> larger FP
2919 } else {
2920 return BitCast; // same size, no-op cast
2921 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00002922 } else if (SrcTy->isVectorTy()) {
2923 assert(DestBits == SrcBits &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002924 "Casting vector to floating point of different width");
Devang Patele9432132008-11-05 01:37:40 +00002925 return BitCast; // same size, no-op cast
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002926 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002927 llvm_unreachable("Casting pointer or non-first class to float");
Duncan Sands27bd0df2011-05-18 10:59:25 +00002928 } else if (DestTy->isVectorTy()) {
2929 assert(DestBits == SrcBits &&
2930 "Illegal cast to vector (wrong type or size)");
2931 return BitCast;
Duncan Sands19d0b472010-02-16 11:11:14 +00002932 } else if (DestTy->isPointerTy()) {
2933 if (SrcTy->isPointerTy()) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002934 if (DestTy->getPointerAddressSpace() != SrcTy->getPointerAddressSpace())
2935 return AddrSpaceCast;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002936 return BitCast; // ptr -> ptr
Duncan Sands9dff9be2010-02-15 16:12:20 +00002937 } else if (SrcTy->isIntegerTy()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002938 return IntToPtr; // int -> ptr
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002939 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002940 llvm_unreachable("Casting pointer to other than pointer or int");
Dale Johannesendd224d22010-09-30 23:57:10 +00002941 } else if (DestTy->isX86_MMXTy()) {
Duncan Sands27bd0df2011-05-18 10:59:25 +00002942 if (SrcTy->isVectorTy()) {
2943 assert(DestBits == SrcBits && "Casting vector of wrong width to X86_MMX");
Dale Johannesendd224d22010-09-30 23:57:10 +00002944 return BitCast; // 64-bit vector to MMX
Dale Johannesendd224d22010-09-30 23:57:10 +00002945 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002946 llvm_unreachable("Illegal cast to X86_MMX");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002947 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002948 llvm_unreachable("Casting to type that is not first-class");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002949}
2950
2951//===----------------------------------------------------------------------===//
2952// CastInst SubClass Constructors
2953//===----------------------------------------------------------------------===//
2954
2955/// Check that the construction parameters for a CastInst are correct. This
2956/// could be broken out into the separate constructors but it is useful to have
2957/// it in one place and to eliminate the redundant code for getting the sizes
2958/// of the types involved.
Fangrui Songf78650a2018-07-30 19:41:25 +00002959bool
Chris Lattner229907c2011-07-18 04:54:35 +00002960CastInst::castIsValid(Instruction::CastOps op, Value *S, Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002961 // Check for type sanity on the arguments
Chris Lattner229907c2011-07-18 04:54:35 +00002962 Type *SrcTy = S->getType();
Evan Cheng098d7b72013-01-10 23:22:53 +00002963
Chris Lattner37bc78a2010-01-26 21:51:43 +00002964 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType() ||
2965 SrcTy->isAggregateType() || DstTy->isAggregateType())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002966 return false;
2967
2968 // Get the size of the types in bits, we'll need this later
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002969 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
2970 unsigned DstBitSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002971
Duncan Sands7f646562011-05-18 09:21:57 +00002972 // If these are vector types, get the lengths of the vectors (using zero for
2973 // scalar types means that checking that vector lengths match also checks that
2974 // scalars are not being converted to vectors or vectors to scalars).
2975 unsigned SrcLength = SrcTy->isVectorTy() ?
2976 cast<VectorType>(SrcTy)->getNumElements() : 0;
2977 unsigned DstLength = DstTy->isVectorTy() ?
2978 cast<VectorType>(DstTy)->getNumElements() : 0;
2979
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002980 // Switch on the opcode provided
2981 switch (op) {
2982 default: return false; // This is an input error
2983 case Instruction::Trunc:
Duncan Sands7f646562011-05-18 09:21:57 +00002984 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
2985 SrcLength == DstLength && SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002986 case Instruction::ZExt:
Duncan Sands7f646562011-05-18 09:21:57 +00002987 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
2988 SrcLength == DstLength && SrcBitSize < DstBitSize;
Fangrui Songf78650a2018-07-30 19:41:25 +00002989 case Instruction::SExt:
Duncan Sands7f646562011-05-18 09:21:57 +00002990 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
2991 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002992 case Instruction::FPTrunc:
Duncan Sands7f646562011-05-18 09:21:57 +00002993 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
2994 SrcLength == DstLength && SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002995 case Instruction::FPExt:
Duncan Sands7f646562011-05-18 09:21:57 +00002996 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
2997 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002998 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002999 case Instruction::SIToFP:
Duncan Sands7f646562011-05-18 09:21:57 +00003000 return SrcTy->isIntOrIntVectorTy() && DstTy->isFPOrFPVectorTy() &&
3001 SrcLength == DstLength;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003002 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003003 case Instruction::FPToSI:
Duncan Sands7f646562011-05-18 09:21:57 +00003004 return SrcTy->isFPOrFPVectorTy() && DstTy->isIntOrIntVectorTy() &&
3005 SrcLength == DstLength;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003006 case Instruction::PtrToInt:
Chris Lattner8a3df542012-01-25 01:32:59 +00003007 if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy))
Nadav Rotem3924cb02011-12-05 06:29:09 +00003008 return false;
Chris Lattner8a3df542012-01-25 01:32:59 +00003009 if (VectorType *VT = dyn_cast<VectorType>(SrcTy))
3010 if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements())
3011 return false;
Craig Topper95d23472017-07-09 07:04:00 +00003012 return SrcTy->isPtrOrPtrVectorTy() && DstTy->isIntOrIntVectorTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003013 case Instruction::IntToPtr:
Chris Lattner8a3df542012-01-25 01:32:59 +00003014 if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy))
Nadav Rotem3924cb02011-12-05 06:29:09 +00003015 return false;
Chris Lattner8a3df542012-01-25 01:32:59 +00003016 if (VectorType *VT = dyn_cast<VectorType>(SrcTy))
3017 if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements())
3018 return false;
Craig Topper95d23472017-07-09 07:04:00 +00003019 return SrcTy->isIntOrIntVectorTy() && DstTy->isPtrOrPtrVectorTy();
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003020 case Instruction::BitCast: {
3021 PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy->getScalarType());
3022 PointerType *DstPtrTy = dyn_cast<PointerType>(DstTy->getScalarType());
3023
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003024 // BitCast implies a no-op cast of type only. No bits change.
3025 // However, you can't cast pointers to anything but pointers.
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003026 if (!SrcPtrTy != !DstPtrTy)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003027 return false;
3028
Alp Tokerf907b892013-12-05 05:44:44 +00003029 // For non-pointer cases, the cast is okay if the source and destination bit
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003030 // widths are identical.
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003031 if (!SrcPtrTy)
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003032 return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits();
3033
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003034 // If both are pointers then the address spaces must match.
3035 if (SrcPtrTy->getAddressSpace() != DstPtrTy->getAddressSpace())
3036 return false;
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003037
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003038 // A vector of pointers must have the same number of elements.
Serguei Katkov09ab5062018-08-21 04:27:07 +00003039 VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy);
3040 VectorType *DstVecTy = dyn_cast<VectorType>(DstTy);
3041 if (SrcVecTy && DstVecTy)
3042 return (SrcVecTy->getNumElements() == DstVecTy->getNumElements());
3043 if (SrcVecTy)
3044 return SrcVecTy->getNumElements() == 1;
3045 if (DstVecTy)
3046 return DstVecTy->getNumElements() == 1;
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003047
3048 return true;
3049 }
3050 case Instruction::AddrSpaceCast: {
3051 PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy->getScalarType());
3052 if (!SrcPtrTy)
3053 return false;
3054
3055 PointerType *DstPtrTy = dyn_cast<PointerType>(DstTy->getScalarType());
3056 if (!DstPtrTy)
3057 return false;
3058
3059 if (SrcPtrTy->getAddressSpace() == DstPtrTy->getAddressSpace())
3060 return false;
3061
3062 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) {
3063 if (VectorType *DstVecTy = dyn_cast<VectorType>(DstTy))
3064 return (SrcVecTy->getNumElements() == DstVecTy->getNumElements());
3065
3066 return false;
3067 }
3068
3069 return true;
3070 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003071 }
3072}
3073
3074TruncInst::TruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003075 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003076) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003077 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003078}
3079
3080TruncInst::TruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003081 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Fangrui Songf78650a2018-07-30 19:41:25 +00003082) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003083 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003084}
3085
3086ZExtInst::ZExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003087 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Fangrui Songf78650a2018-07-30 19:41:25 +00003088) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003089 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003090}
3091
3092ZExtInst::ZExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003093 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Fangrui Songf78650a2018-07-30 19:41:25 +00003094) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003095 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003096}
3097SExtInst::SExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003098 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Fangrui Songf78650a2018-07-30 19:41:25 +00003099) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003100 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003101}
3102
Jeff Cohencc08c832006-12-02 02:22:01 +00003103SExtInst::SExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003104 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Fangrui Songf78650a2018-07-30 19:41:25 +00003105) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003106 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003107}
3108
3109FPTruncInst::FPTruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003110 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Fangrui Songf78650a2018-07-30 19:41:25 +00003111) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003112 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003113}
3114
3115FPTruncInst::FPTruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003116 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Fangrui Songf78650a2018-07-30 19:41:25 +00003117) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003118 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003119}
3120
3121FPExtInst::FPExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003122 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Fangrui Songf78650a2018-07-30 19:41:25 +00003123) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003124 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003125}
3126
3127FPExtInst::FPExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003128 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Fangrui Songf78650a2018-07-30 19:41:25 +00003129) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003130 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003131}
3132
3133UIToFPInst::UIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003134 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Fangrui Songf78650a2018-07-30 19:41:25 +00003135) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003136 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003137}
3138
3139UIToFPInst::UIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003140 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Fangrui Songf78650a2018-07-30 19:41:25 +00003141) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003142 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003143}
3144
3145SIToFPInst::SIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003146 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Fangrui Songf78650a2018-07-30 19:41:25 +00003147) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003148 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003149}
3150
3151SIToFPInst::SIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003152 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Fangrui Songf78650a2018-07-30 19:41:25 +00003153) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003154 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003155}
3156
3157FPToUIInst::FPToUIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003158 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Fangrui Songf78650a2018-07-30 19:41:25 +00003159) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003160 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003161}
3162
3163FPToUIInst::FPToUIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003164 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Fangrui Songf78650a2018-07-30 19:41:25 +00003165) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003166 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003167}
3168
3169FPToSIInst::FPToSIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003170 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Fangrui Songf78650a2018-07-30 19:41:25 +00003171) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003172 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003173}
3174
3175FPToSIInst::FPToSIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003176 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Fangrui Songf78650a2018-07-30 19:41:25 +00003177) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003178 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003179}
3180
3181PtrToIntInst::PtrToIntInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003182 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Fangrui Songf78650a2018-07-30 19:41:25 +00003183) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003184 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003185}
3186
3187PtrToIntInst::PtrToIntInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003188 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Fangrui Songf78650a2018-07-30 19:41:25 +00003189) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003190 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003191}
3192
3193IntToPtrInst::IntToPtrInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003194 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Fangrui Songf78650a2018-07-30 19:41:25 +00003195) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003196 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003197}
3198
3199IntToPtrInst::IntToPtrInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003200 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Fangrui Songf78650a2018-07-30 19:41:25 +00003201) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003202 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003203}
3204
3205BitCastInst::BitCastInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003206 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Fangrui Songf78650a2018-07-30 19:41:25 +00003207) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003208 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003209}
3210
3211BitCastInst::BitCastInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003212 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Fangrui Songf78650a2018-07-30 19:41:25 +00003213) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003214 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003215}
Chris Lattnerf16dc002006-09-17 19:29:56 +00003216
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003217AddrSpaceCastInst::AddrSpaceCastInst(
3218 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
3219) : CastInst(Ty, AddrSpaceCast, S, Name, InsertBefore) {
3220 assert(castIsValid(getOpcode(), S, Ty) && "Illegal AddrSpaceCast");
3221}
3222
3223AddrSpaceCastInst::AddrSpaceCastInst(
3224 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
3225) : CastInst(Ty, AddrSpaceCast, S, Name, InsertAtEnd) {
3226 assert(castIsValid(getOpcode(), S, Ty) && "Illegal AddrSpaceCast");
3227}
3228
Chris Lattnerf16dc002006-09-17 19:29:56 +00003229//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00003230// CmpInst Classes
3231//===----------------------------------------------------------------------===//
3232
Craig Topper1c3f2832015-12-15 06:11:33 +00003233CmpInst::CmpInst(Type *ty, OtherOps op, Predicate predicate, Value *LHS,
Sanjay Pateld1172a02018-11-07 00:00:42 +00003234 Value *RHS, const Twine &Name, Instruction *InsertBefore,
3235 Instruction *FlagsSource)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00003236 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00003237 OperandTraits<CmpInst>::op_begin(this),
3238 OperandTraits<CmpInst>::operands(this),
3239 InsertBefore) {
Sanjay Pateld1172a02018-11-07 00:00:42 +00003240 Op<0>() = LHS;
3241 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00003242 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00003243 setName(Name);
Sanjay Pateld1172a02018-11-07 00:00:42 +00003244 if (FlagsSource)
3245 copyIRFlags(FlagsSource);
Reid Spencerd9436b62006-11-20 01:22:35 +00003246}
Gabor Greiff6caff662008-05-10 08:32:32 +00003247
Craig Topper1c3f2832015-12-15 06:11:33 +00003248CmpInst::CmpInst(Type *ty, OtherOps op, Predicate predicate, Value *LHS,
3249 Value *RHS, const Twine &Name, BasicBlock *InsertAtEnd)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00003250 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00003251 OperandTraits<CmpInst>::op_begin(this),
3252 OperandTraits<CmpInst>::operands(this),
3253 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003254 Op<0>() = LHS;
3255 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00003256 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00003257 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003258}
3259
3260CmpInst *
Craig Topper1c3f2832015-12-15 06:11:33 +00003261CmpInst::Create(OtherOps Op, Predicate predicate, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00003262 const Twine &Name, Instruction *InsertBefore) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003263 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003264 if (InsertBefore)
3265 return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate),
3266 S1, S2, Name);
3267 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003268 return new ICmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003269 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003270 }
Fangrui Songf78650a2018-07-30 19:41:25 +00003271
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003272 if (InsertBefore)
3273 return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate),
3274 S1, S2, Name);
3275 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003276 return new FCmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003277 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003278}
3279
3280CmpInst *
Craig Topper1c3f2832015-12-15 06:11:33 +00003281CmpInst::Create(OtherOps Op, Predicate predicate, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00003282 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003283 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003284 return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
3285 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003286 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003287 return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
3288 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003289}
3290
3291void CmpInst::swapOperands() {
3292 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
3293 IC->swapOperands();
3294 else
3295 cast<FCmpInst>(this)->swapOperands();
3296}
3297
Duncan Sands95c4ecc2011-01-04 12:52:29 +00003298bool CmpInst::isCommutative() const {
3299 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00003300 return IC->isCommutative();
3301 return cast<FCmpInst>(this)->isCommutative();
3302}
3303
Duncan Sands95c4ecc2011-01-04 12:52:29 +00003304bool CmpInst::isEquality() const {
3305 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00003306 return IC->isEquality();
3307 return cast<FCmpInst>(this)->isEquality();
3308}
3309
Dan Gohman4e724382008-05-31 02:47:54 +00003310CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003311 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003312 default: llvm_unreachable("Unknown cmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00003313 case ICMP_EQ: return ICMP_NE;
3314 case ICMP_NE: return ICMP_EQ;
3315 case ICMP_UGT: return ICMP_ULE;
3316 case ICMP_ULT: return ICMP_UGE;
3317 case ICMP_UGE: return ICMP_ULT;
3318 case ICMP_ULE: return ICMP_UGT;
3319 case ICMP_SGT: return ICMP_SLE;
3320 case ICMP_SLT: return ICMP_SGE;
3321 case ICMP_SGE: return ICMP_SLT;
3322 case ICMP_SLE: return ICMP_SGT;
Reid Spencerd9436b62006-11-20 01:22:35 +00003323
Dan Gohman4e724382008-05-31 02:47:54 +00003324 case FCMP_OEQ: return FCMP_UNE;
3325 case FCMP_ONE: return FCMP_UEQ;
3326 case FCMP_OGT: return FCMP_ULE;
3327 case FCMP_OLT: return FCMP_UGE;
3328 case FCMP_OGE: return FCMP_ULT;
3329 case FCMP_OLE: return FCMP_UGT;
3330 case FCMP_UEQ: return FCMP_ONE;
3331 case FCMP_UNE: return FCMP_OEQ;
3332 case FCMP_UGT: return FCMP_OLE;
3333 case FCMP_ULT: return FCMP_OGE;
3334 case FCMP_UGE: return FCMP_OLT;
3335 case FCMP_ULE: return FCMP_OGT;
3336 case FCMP_ORD: return FCMP_UNO;
3337 case FCMP_UNO: return FCMP_ORD;
3338 case FCMP_TRUE: return FCMP_FALSE;
3339 case FCMP_FALSE: return FCMP_TRUE;
Reid Spencerd9436b62006-11-20 01:22:35 +00003340 }
3341}
3342
Tim Northoverde3aea0412016-08-17 20:25:25 +00003343StringRef CmpInst::getPredicateName(Predicate Pred) {
3344 switch (Pred) {
3345 default: return "unknown";
3346 case FCmpInst::FCMP_FALSE: return "false";
3347 case FCmpInst::FCMP_OEQ: return "oeq";
3348 case FCmpInst::FCMP_OGT: return "ogt";
3349 case FCmpInst::FCMP_OGE: return "oge";
3350 case FCmpInst::FCMP_OLT: return "olt";
3351 case FCmpInst::FCMP_OLE: return "ole";
3352 case FCmpInst::FCMP_ONE: return "one";
3353 case FCmpInst::FCMP_ORD: return "ord";
3354 case FCmpInst::FCMP_UNO: return "uno";
3355 case FCmpInst::FCMP_UEQ: return "ueq";
3356 case FCmpInst::FCMP_UGT: return "ugt";
3357 case FCmpInst::FCMP_UGE: return "uge";
3358 case FCmpInst::FCMP_ULT: return "ult";
3359 case FCmpInst::FCMP_ULE: return "ule";
3360 case FCmpInst::FCMP_UNE: return "une";
3361 case FCmpInst::FCMP_TRUE: return "true";
3362 case ICmpInst::ICMP_EQ: return "eq";
3363 case ICmpInst::ICMP_NE: return "ne";
3364 case ICmpInst::ICMP_SGT: return "sgt";
3365 case ICmpInst::ICMP_SGE: return "sge";
3366 case ICmpInst::ICMP_SLT: return "slt";
3367 case ICmpInst::ICMP_SLE: return "sle";
3368 case ICmpInst::ICMP_UGT: return "ugt";
3369 case ICmpInst::ICMP_UGE: return "uge";
3370 case ICmpInst::ICMP_ULT: return "ult";
3371 case ICmpInst::ICMP_ULE: return "ule";
3372 }
3373}
3374
Reid Spencer266e42b2006-12-23 06:05:41 +00003375ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
3376 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003377 default: llvm_unreachable("Unknown icmp predicate!");
Fangrui Songf78650a2018-07-30 19:41:25 +00003378 case ICMP_EQ: case ICMP_NE:
3379 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
Reid Spencer266e42b2006-12-23 06:05:41 +00003380 return pred;
3381 case ICMP_UGT: return ICMP_SGT;
3382 case ICMP_ULT: return ICMP_SLT;
3383 case ICMP_UGE: return ICMP_SGE;
3384 case ICMP_ULE: return ICMP_SLE;
3385 }
3386}
3387
Nick Lewycky8ea81e82008-01-28 03:48:02 +00003388ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
3389 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003390 default: llvm_unreachable("Unknown icmp predicate!");
Fangrui Songf78650a2018-07-30 19:41:25 +00003391 case ICMP_EQ: case ICMP_NE:
3392 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
Nick Lewycky8ea81e82008-01-28 03:48:02 +00003393 return pred;
3394 case ICMP_SGT: return ICMP_UGT;
3395 case ICMP_SLT: return ICMP_ULT;
3396 case ICMP_SGE: return ICMP_UGE;
3397 case ICMP_SLE: return ICMP_ULE;
3398 }
3399}
3400
Serguei Katkov3cb4c342018-02-09 07:59:07 +00003401CmpInst::Predicate CmpInst::getFlippedStrictnessPredicate(Predicate pred) {
3402 switch (pred) {
3403 default: llvm_unreachable("Unknown or unsupported cmp predicate!");
3404 case ICMP_SGT: return ICMP_SGE;
3405 case ICMP_SLT: return ICMP_SLE;
3406 case ICMP_SGE: return ICMP_SGT;
3407 case ICMP_SLE: return ICMP_SLT;
3408 case ICMP_UGT: return ICMP_UGE;
3409 case ICMP_ULT: return ICMP_ULE;
3410 case ICMP_UGE: return ICMP_UGT;
3411 case ICMP_ULE: return ICMP_ULT;
3412
3413 case FCMP_OGT: return FCMP_OGE;
3414 case FCMP_OLT: return FCMP_OLE;
3415 case FCMP_OGE: return FCMP_OGT;
3416 case FCMP_OLE: return FCMP_OLT;
3417 case FCMP_UGT: return FCMP_UGE;
3418 case FCMP_ULT: return FCMP_ULE;
3419 case FCMP_UGE: return FCMP_UGT;
3420 case FCMP_ULE: return FCMP_ULT;
3421 }
3422}
3423
Dan Gohman4e724382008-05-31 02:47:54 +00003424CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003425 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003426 default: llvm_unreachable("Unknown cmp predicate!");
Dan Gohman4e724382008-05-31 02:47:54 +00003427 case ICMP_EQ: case ICMP_NE:
3428 return pred;
3429 case ICMP_SGT: return ICMP_SLT;
3430 case ICMP_SLT: return ICMP_SGT;
3431 case ICMP_SGE: return ICMP_SLE;
3432 case ICMP_SLE: return ICMP_SGE;
3433 case ICMP_UGT: return ICMP_ULT;
3434 case ICMP_ULT: return ICMP_UGT;
3435 case ICMP_UGE: return ICMP_ULE;
3436 case ICMP_ULE: return ICMP_UGE;
Fangrui Songf78650a2018-07-30 19:41:25 +00003437
Reid Spencerd9436b62006-11-20 01:22:35 +00003438 case FCMP_FALSE: case FCMP_TRUE:
3439 case FCMP_OEQ: case FCMP_ONE:
3440 case FCMP_UEQ: case FCMP_UNE:
3441 case FCMP_ORD: case FCMP_UNO:
3442 return pred;
3443 case FCMP_OGT: return FCMP_OLT;
3444 case FCMP_OLT: return FCMP_OGT;
3445 case FCMP_OGE: return FCMP_OLE;
3446 case FCMP_OLE: return FCMP_OGE;
3447 case FCMP_UGT: return FCMP_ULT;
3448 case FCMP_ULT: return FCMP_UGT;
3449 case FCMP_UGE: return FCMP_ULE;
3450 case FCMP_ULE: return FCMP_UGE;
3451 }
3452}
3453
Max Kazantsevb299ade2018-02-07 11:16:29 +00003454CmpInst::Predicate CmpInst::getNonStrictPredicate(Predicate pred) {
3455 switch (pred) {
3456 case ICMP_SGT: return ICMP_SGE;
3457 case ICMP_SLT: return ICMP_SLE;
3458 case ICMP_UGT: return ICMP_UGE;
3459 case ICMP_ULT: return ICMP_ULE;
3460 case FCMP_OGT: return FCMP_OGE;
3461 case FCMP_OLT: return FCMP_OLE;
3462 case FCMP_UGT: return FCMP_UGE;
3463 case FCMP_ULT: return FCMP_ULE;
3464 default: return pred;
3465 }
3466}
3467
Sanjoy Das6e78b172015-10-22 19:57:34 +00003468CmpInst::Predicate CmpInst::getSignedPredicate(Predicate pred) {
3469 assert(CmpInst::isUnsigned(pred) && "Call only with signed predicates!");
3470
3471 switch (pred) {
3472 default:
3473 llvm_unreachable("Unknown predicate!");
3474 case CmpInst::ICMP_ULT:
3475 return CmpInst::ICMP_SLT;
3476 case CmpInst::ICMP_ULE:
3477 return CmpInst::ICMP_SLE;
3478 case CmpInst::ICMP_UGT:
3479 return CmpInst::ICMP_SGT;
3480 case CmpInst::ICMP_UGE:
3481 return CmpInst::ICMP_SGE;
3482 }
3483}
3484
Craig Topper1c3f2832015-12-15 06:11:33 +00003485bool CmpInst::isUnsigned(Predicate predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003486 switch (predicate) {
3487 default: return false;
Fangrui Songf78650a2018-07-30 19:41:25 +00003488 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
Reid Spencer266e42b2006-12-23 06:05:41 +00003489 case ICmpInst::ICMP_UGE: return true;
3490 }
3491}
3492
Craig Topper1c3f2832015-12-15 06:11:33 +00003493bool CmpInst::isSigned(Predicate predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003494 switch (predicate) {
3495 default: return false;
Fangrui Songf78650a2018-07-30 19:41:25 +00003496 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
Reid Spencer266e42b2006-12-23 06:05:41 +00003497 case ICmpInst::ICMP_SGE: return true;
3498 }
3499}
3500
Craig Topper1c3f2832015-12-15 06:11:33 +00003501bool CmpInst::isOrdered(Predicate predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003502 switch (predicate) {
3503 default: return false;
Fangrui Songf78650a2018-07-30 19:41:25 +00003504 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
3505 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
Reid Spencer266e42b2006-12-23 06:05:41 +00003506 case FCmpInst::FCMP_ORD: return true;
3507 }
3508}
Fangrui Songf78650a2018-07-30 19:41:25 +00003509
Craig Topper1c3f2832015-12-15 06:11:33 +00003510bool CmpInst::isUnordered(Predicate predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003511 switch (predicate) {
3512 default: return false;
Fangrui Songf78650a2018-07-30 19:41:25 +00003513 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
3514 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
Reid Spencer266e42b2006-12-23 06:05:41 +00003515 case FCmpInst::FCMP_UNO: return true;
3516 }
3517}
3518
Craig Topper1c3f2832015-12-15 06:11:33 +00003519bool CmpInst::isTrueWhenEqual(Predicate predicate) {
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003520 switch(predicate) {
3521 default: return false;
3522 case ICMP_EQ: case ICMP_UGE: case ICMP_ULE: case ICMP_SGE: case ICMP_SLE:
3523 case FCMP_TRUE: case FCMP_UEQ: case FCMP_UGE: case FCMP_ULE: return true;
3524 }
3525}
3526
Craig Topper1c3f2832015-12-15 06:11:33 +00003527bool CmpInst::isFalseWhenEqual(Predicate predicate) {
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003528 switch(predicate) {
3529 case ICMP_NE: case ICMP_UGT: case ICMP_ULT: case ICMP_SGT: case ICMP_SLT:
3530 case FCMP_FALSE: case FCMP_ONE: case FCMP_OGT: case FCMP_OLT: return true;
3531 default: return false;
3532 }
3533}
3534
Chad Rosier99bc4802016-04-21 16:18:02 +00003535bool CmpInst::isImpliedTrueByMatchingCmp(Predicate Pred1, Predicate Pred2) {
Chad Rosieraf83e402016-04-21 14:04:54 +00003536 // If the predicates match, then we know the first condition implies the
3537 // second is true.
3538 if (Pred1 == Pred2)
3539 return true;
3540
3541 switch (Pred1) {
3542 default:
3543 break;
Chad Rosier1a601592016-04-22 17:57:34 +00003544 case ICMP_EQ:
Chad Rosier3d75f8c2016-04-25 13:25:14 +00003545 // 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 +00003546 return Pred2 == ICMP_UGE || Pred2 == ICMP_ULE || Pred2 == ICMP_SGE ||
3547 Pred2 == ICMP_SLE;
Chad Rosier3456cb52016-04-22 17:14:12 +00003548 case ICMP_UGT: // A >u B implies A != B and A >=u B are true.
3549 return Pred2 == ICMP_NE || Pred2 == ICMP_UGE;
3550 case ICMP_ULT: // A <u B implies A != B and A <=u B are true.
3551 return Pred2 == ICMP_NE || Pred2 == ICMP_ULE;
3552 case ICMP_SGT: // A >s B implies A != B and A >=s B are true.
3553 return Pred2 == ICMP_NE || Pred2 == ICMP_SGE;
3554 case ICMP_SLT: // A <s B implies A != B and A <=s B are true.
3555 return Pred2 == ICMP_NE || Pred2 == ICMP_SLE;
Chad Rosieraf83e402016-04-21 14:04:54 +00003556 }
3557 return false;
3558}
3559
Chad Rosier99bc4802016-04-21 16:18:02 +00003560bool CmpInst::isImpliedFalseByMatchingCmp(Predicate Pred1, Predicate Pred2) {
Chad Rosier1a601592016-04-22 17:57:34 +00003561 return isImpliedTrueByMatchingCmp(Pred1, getInversePredicate(Pred2));
Chad Rosieraf83e402016-04-21 14:04:54 +00003562}
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003563
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003564//===----------------------------------------------------------------------===//
3565// SwitchInst Implementation
3566//===----------------------------------------------------------------------===//
3567
Chris Lattnerbaf00152010-11-17 05:41:46 +00003568void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumReserved) {
3569 assert(Value && Default && NumReserved);
3570 ReservedSpace = NumReserved;
Pete Cooperb4eede22015-06-12 17:48:10 +00003571 setNumHungOffUseOperands(2);
Pete Cooper3fc30402015-06-10 22:38:46 +00003572 allocHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003573
Pete Coopereb31b682015-05-21 22:48:54 +00003574 Op<0>() = Value;
3575 Op<1>() = Default;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003576}
3577
Chris Lattner2195fc42007-02-24 00:55:48 +00003578/// SwitchInst ctor - Create a new switch instruction, specifying a value to
3579/// switch on and a default destination. The number of additional cases can
3580/// be specified here to make memory allocation more efficient. This
3581/// constructor can also autoinsert before another instruction.
3582SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3583 Instruction *InsertBefore)
Chandler Carruth509e20e2018-10-19 00:22:37 +00003584 : Instruction(Type::getVoidTy(Value->getContext()), Instruction::Switch,
3585 nullptr, 0, InsertBefore) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003586 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00003587}
3588
3589/// SwitchInst ctor - Create a new switch instruction, specifying a value to
3590/// switch on and a default destination. The number of additional cases can
3591/// be specified here to make memory allocation more efficient. This
3592/// constructor also autoinserts at the end of the specified BasicBlock.
3593SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3594 BasicBlock *InsertAtEnd)
Chandler Carruth509e20e2018-10-19 00:22:37 +00003595 : Instruction(Type::getVoidTy(Value->getContext()), Instruction::Switch,
3596 nullptr, 0, InsertAtEnd) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003597 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00003598}
3599
Misha Brukmanb1c93172005-04-21 23:48:37 +00003600SwitchInst::SwitchInst(const SwitchInst &SI)
Chandler Carruth509e20e2018-10-19 00:22:37 +00003601 : Instruction(SI.getType(), Instruction::Switch, nullptr, 0) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003602 init(SI.getCondition(), SI.getDefaultDest(), SI.getNumOperands());
Pete Cooperb4eede22015-06-12 17:48:10 +00003603 setNumHungOffUseOperands(SI.getNumOperands());
Pete Cooper74510a42015-06-12 17:48:05 +00003604 Use *OL = getOperandList();
3605 const Use *InOL = SI.getOperandList();
Chris Lattnerbaf00152010-11-17 05:41:46 +00003606 for (unsigned i = 2, E = SI.getNumOperands(); i != E; i += 2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003607 OL[i] = InOL[i];
3608 OL[i+1] = InOL[i+1];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003609 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003610 SubclassOptionalData = SI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003611}
3612
3613/// addCase - Add an entry to the switch instruction...
3614///
Chris Lattner47ac1872005-02-24 05:32:09 +00003615void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Pete Cooperb4eede22015-06-12 17:48:10 +00003616 unsigned NewCaseIdx = getNumCases();
3617 unsigned OpNo = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003618 if (OpNo+2 > ReservedSpace)
Jay Foade98f29d2011-04-01 08:00:58 +00003619 growOperands(); // Get more space!
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003620 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00003621 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Pete Cooperb4eede22015-06-12 17:48:10 +00003622 setNumHungOffUseOperands(OpNo+2);
Chandler Carruth927d8e62017-04-12 07:27:28 +00003623 CaseHandle Case(this, NewCaseIdx);
Bob Wilsone4077362013-09-09 19:14:35 +00003624 Case.setValue(OnVal);
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00003625 Case.setSuccessor(Dest);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003626}
3627
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003628/// removeCase - This method removes the specified case and its successor
3629/// from the switch instruction.
Chandler Carruth927d8e62017-04-12 07:27:28 +00003630SwitchInst::CaseIt SwitchInst::removeCase(CaseIt I) {
3631 unsigned idx = I->getCaseIndex();
3632
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003633 assert(2 + idx*2 < getNumOperands() && "Case index out of range!!!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003634
3635 unsigned NumOps = getNumOperands();
Pete Cooper74510a42015-06-12 17:48:05 +00003636 Use *OL = getOperandList();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003637
Jay Foad14277722011-02-01 09:22:34 +00003638 // Overwrite this case with the end of the list.
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003639 if (2 + (idx + 1) * 2 != NumOps) {
3640 OL[2 + idx * 2] = OL[NumOps - 2];
3641 OL[2 + idx * 2 + 1] = OL[NumOps - 1];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003642 }
3643
3644 // Nuke the last value.
Craig Topperc6207612014-04-09 06:08:46 +00003645 OL[NumOps-2].set(nullptr);
3646 OL[NumOps-2+1].set(nullptr);
Pete Cooperb4eede22015-06-12 17:48:10 +00003647 setNumHungOffUseOperands(NumOps-2);
Chandler Carruth0d256c02017-03-26 02:49:23 +00003648
3649 return CaseIt(this, idx);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003650}
3651
Jay Foade98f29d2011-04-01 08:00:58 +00003652/// growOperands - grow operands - This grows the operand list in response
3653/// to a push_back style of operation. This grows the number of ops by 3 times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003654///
Jay Foade98f29d2011-04-01 08:00:58 +00003655void SwitchInst::growOperands() {
Gabor Greiff6caff662008-05-10 08:32:32 +00003656 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +00003657 unsigned NumOps = e*3;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003658
3659 ReservedSpace = NumOps;
Pete Cooper93f9ff52015-06-10 22:38:41 +00003660 growHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003661}
3662
Chris Lattner3ed871f2009-10-27 19:13:16 +00003663//===----------------------------------------------------------------------===//
Jay Foadbbb91f22011-01-16 15:30:52 +00003664// IndirectBrInst Implementation
Chris Lattner3ed871f2009-10-27 19:13:16 +00003665//===----------------------------------------------------------------------===//
3666
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003667void IndirectBrInst::init(Value *Address, unsigned NumDests) {
Duncan Sands19d0b472010-02-16 11:11:14 +00003668 assert(Address && Address->getType()->isPointerTy() &&
Chris Lattner6747b4c2009-10-29 05:53:32 +00003669 "Address of indirectbr must be a pointer");
Chris Lattner3ed871f2009-10-27 19:13:16 +00003670 ReservedSpace = 1+NumDests;
Pete Cooperb4eede22015-06-12 17:48:10 +00003671 setNumHungOffUseOperands(1);
Pete Cooper3fc30402015-06-10 22:38:46 +00003672 allocHungoffUses(ReservedSpace);
3673
Pete Coopereb31b682015-05-21 22:48:54 +00003674 Op<0>() = Address;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003675}
3676
3677
Jay Foade98f29d2011-04-01 08:00:58 +00003678/// growOperands - grow operands - This grows the operand list in response
3679/// to a push_back style of operation. This grows the number of ops by 2 times.
Chris Lattner3ed871f2009-10-27 19:13:16 +00003680///
Jay Foade98f29d2011-04-01 08:00:58 +00003681void IndirectBrInst::growOperands() {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003682 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +00003683 unsigned NumOps = e*2;
Fangrui Songf78650a2018-07-30 19:41:25 +00003684
Chris Lattner3ed871f2009-10-27 19:13:16 +00003685 ReservedSpace = NumOps;
Pete Cooper93f9ff52015-06-10 22:38:41 +00003686 growHungoffUses(ReservedSpace);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003687}
3688
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003689IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3690 Instruction *InsertBefore)
Chandler Carruth509e20e2018-10-19 00:22:37 +00003691 : Instruction(Type::getVoidTy(Address->getContext()),
3692 Instruction::IndirectBr, nullptr, 0, InsertBefore) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003693 init(Address, NumCases);
3694}
3695
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003696IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3697 BasicBlock *InsertAtEnd)
Chandler Carruth509e20e2018-10-19 00:22:37 +00003698 : Instruction(Type::getVoidTy(Address->getContext()),
3699 Instruction::IndirectBr, nullptr, 0, InsertAtEnd) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003700 init(Address, NumCases);
3701}
3702
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003703IndirectBrInst::IndirectBrInst(const IndirectBrInst &IBI)
Chandler Carruth509e20e2018-10-19 00:22:37 +00003704 : Instruction(Type::getVoidTy(IBI.getContext()), Instruction::IndirectBr,
3705 nullptr, IBI.getNumOperands()) {
Pete Cooper3fc30402015-06-10 22:38:46 +00003706 allocHungoffUses(IBI.getNumOperands());
Pete Cooper74510a42015-06-12 17:48:05 +00003707 Use *OL = getOperandList();
3708 const Use *InOL = IBI.getOperandList();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003709 for (unsigned i = 0, E = IBI.getNumOperands(); i != E; ++i)
3710 OL[i] = InOL[i];
3711 SubclassOptionalData = IBI.SubclassOptionalData;
3712}
3713
Chris Lattner3ed871f2009-10-27 19:13:16 +00003714/// addDestination - Add a destination.
3715///
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003716void IndirectBrInst::addDestination(BasicBlock *DestBB) {
Pete Cooperb4eede22015-06-12 17:48:10 +00003717 unsigned OpNo = getNumOperands();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003718 if (OpNo+1 > ReservedSpace)
Jay Foade98f29d2011-04-01 08:00:58 +00003719 growOperands(); // Get more space!
Chris Lattner3ed871f2009-10-27 19:13:16 +00003720 // Initialize some new operands.
3721 assert(OpNo < ReservedSpace && "Growing didn't work!");
Pete Cooperb4eede22015-06-12 17:48:10 +00003722 setNumHungOffUseOperands(OpNo+1);
Pete Cooper74510a42015-06-12 17:48:05 +00003723 getOperandList()[OpNo] = DestBB;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003724}
3725
3726/// removeDestination - This method removes the specified successor from the
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003727/// indirectbr instruction.
3728void IndirectBrInst::removeDestination(unsigned idx) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003729 assert(idx < getNumOperands()-1 && "Successor index out of range!");
Fangrui Songf78650a2018-07-30 19:41:25 +00003730
Chris Lattner3ed871f2009-10-27 19:13:16 +00003731 unsigned NumOps = getNumOperands();
Pete Cooper74510a42015-06-12 17:48:05 +00003732 Use *OL = getOperandList();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003733
3734 // Replace this value with the last one.
3735 OL[idx+1] = OL[NumOps-1];
Fangrui Songf78650a2018-07-30 19:41:25 +00003736
Chris Lattner3ed871f2009-10-27 19:13:16 +00003737 // Nuke the last value.
Craig Topperc6207612014-04-09 06:08:46 +00003738 OL[NumOps-1].set(nullptr);
Pete Cooperb4eede22015-06-12 17:48:10 +00003739 setNumHungOffUseOperands(NumOps-1);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003740}
3741
Chris Lattner3ed871f2009-10-27 19:13:16 +00003742//===----------------------------------------------------------------------===//
Pete Cooper75403d72015-06-24 20:22:23 +00003743// cloneImpl() implementations
Chris Lattner3ed871f2009-10-27 19:13:16 +00003744//===----------------------------------------------------------------------===//
3745
Chris Lattnerf22be932004-10-15 23:52:53 +00003746// Define these methods here so vtables don't get emitted into every translation
3747// unit that uses these classes.
3748
Pete Cooper75403d72015-06-24 20:22:23 +00003749GetElementPtrInst *GetElementPtrInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003750 return new (getNumOperands()) GetElementPtrInst(*this);
Chris Lattnerf22be932004-10-15 23:52:53 +00003751}
3752
Cameron McInallycbde0d92018-11-13 18:15:47 +00003753UnaryOperator *UnaryOperator::cloneImpl() const {
3754 return Create(getOpcode(), Op<0>());
3755}
3756
Pete Cooper75403d72015-06-24 20:22:23 +00003757BinaryOperator *BinaryOperator::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003758 return Create(getOpcode(), Op<0>(), Op<1>());
Chris Lattnerf22be932004-10-15 23:52:53 +00003759}
3760
Pete Cooper75403d72015-06-24 20:22:23 +00003761FCmpInst *FCmpInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003762 return new FCmpInst(getPredicate(), Op<0>(), Op<1>());
Reid Spencerd9436b62006-11-20 01:22:35 +00003763}
3764
Pete Cooper75403d72015-06-24 20:22:23 +00003765ICmpInst *ICmpInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003766 return new ICmpInst(getPredicate(), Op<0>(), Op<1>());
Dan Gohman0752bff2008-05-23 00:36:11 +00003767}
3768
Pete Cooper75403d72015-06-24 20:22:23 +00003769ExtractValueInst *ExtractValueInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003770 return new ExtractValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003771}
3772
Pete Cooper75403d72015-06-24 20:22:23 +00003773InsertValueInst *InsertValueInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003774 return new InsertValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003775}
3776
Pete Cooper75403d72015-06-24 20:22:23 +00003777AllocaInst *AllocaInst::cloneImpl() const {
David Majnemer6b3244c2014-04-30 16:12:21 +00003778 AllocaInst *Result = new AllocaInst(getAllocatedType(),
Matt Arsenault3c1fc762017-04-10 22:27:50 +00003779 getType()->getAddressSpace(),
David Majnemer6b3244c2014-04-30 16:12:21 +00003780 (Value *)getOperand(0), getAlignment());
3781 Result->setUsedWithInAlloca(isUsedWithInAlloca());
Manman Ren9bfd0d02016-04-01 21:41:15 +00003782 Result->setSwiftError(isSwiftError());
David Majnemer6b3244c2014-04-30 16:12:21 +00003783 return Result;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003784}
3785
Pete Cooper75403d72015-06-24 20:22:23 +00003786LoadInst *LoadInst::cloneImpl() const {
Eli Friedman59b66882011-08-09 23:02:53 +00003787 return new LoadInst(getOperand(0), Twine(), isVolatile(),
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00003788 getAlignment(), getOrdering(), getSyncScopeID());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003789}
3790
Pete Cooper75403d72015-06-24 20:22:23 +00003791StoreInst *StoreInst::cloneImpl() const {
Eli Friedmancad9f2a2011-08-10 17:39:11 +00003792 return new StoreInst(getOperand(0), getOperand(1), isVolatile(),
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00003793 getAlignment(), getOrdering(), getSyncScopeID());
Fangrui Songf78650a2018-07-30 19:41:25 +00003794
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003795}
3796
Pete Cooper75403d72015-06-24 20:22:23 +00003797AtomicCmpXchgInst *AtomicCmpXchgInst::cloneImpl() const {
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003798 AtomicCmpXchgInst *Result =
3799 new AtomicCmpXchgInst(getOperand(0), getOperand(1), getOperand(2),
Tim Northovere94a5182014-03-11 10:48:52 +00003800 getSuccessOrdering(), getFailureOrdering(),
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00003801 getSyncScopeID());
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003802 Result->setVolatile(isVolatile());
Tim Northover420a2162014-06-13 14:24:07 +00003803 Result->setWeak(isWeak());
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003804 return Result;
3805}
3806
Pete Cooper75403d72015-06-24 20:22:23 +00003807AtomicRMWInst *AtomicRMWInst::cloneImpl() const {
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003808 AtomicRMWInst *Result =
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00003809 new AtomicRMWInst(getOperation(), getOperand(0), getOperand(1),
3810 getOrdering(), getSyncScopeID());
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003811 Result->setVolatile(isVolatile());
3812 return Result;
3813}
3814
Pete Cooper75403d72015-06-24 20:22:23 +00003815FenceInst *FenceInst::cloneImpl() const {
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00003816 return new FenceInst(getContext(), getOrdering(), getSyncScopeID());
Eli Friedmanfee02c62011-07-25 23:16:38 +00003817}
3818
Pete Cooper75403d72015-06-24 20:22:23 +00003819TruncInst *TruncInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003820 return new TruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003821}
3822
Pete Cooper75403d72015-06-24 20:22:23 +00003823ZExtInst *ZExtInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003824 return new ZExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003825}
3826
Pete Cooper75403d72015-06-24 20:22:23 +00003827SExtInst *SExtInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003828 return new SExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003829}
3830
Pete Cooper75403d72015-06-24 20:22:23 +00003831FPTruncInst *FPTruncInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003832 return new FPTruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003833}
3834
Pete Cooper75403d72015-06-24 20:22:23 +00003835FPExtInst *FPExtInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003836 return new FPExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003837}
3838
Pete Cooper75403d72015-06-24 20:22:23 +00003839UIToFPInst *UIToFPInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003840 return new UIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003841}
3842
Pete Cooper75403d72015-06-24 20:22:23 +00003843SIToFPInst *SIToFPInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003844 return new SIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003845}
3846
Pete Cooper75403d72015-06-24 20:22:23 +00003847FPToUIInst *FPToUIInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003848 return new FPToUIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003849}
3850
Pete Cooper75403d72015-06-24 20:22:23 +00003851FPToSIInst *FPToSIInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003852 return new FPToSIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003853}
3854
Pete Cooper75403d72015-06-24 20:22:23 +00003855PtrToIntInst *PtrToIntInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003856 return new PtrToIntInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003857}
3858
Pete Cooper75403d72015-06-24 20:22:23 +00003859IntToPtrInst *IntToPtrInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003860 return new IntToPtrInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003861}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003862
Pete Cooper75403d72015-06-24 20:22:23 +00003863BitCastInst *BitCastInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003864 return new BitCastInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003865}
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003866
Pete Cooper75403d72015-06-24 20:22:23 +00003867AddrSpaceCastInst *AddrSpaceCastInst::cloneImpl() const {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003868 return new AddrSpaceCastInst(getOperand(0), getType());
3869}
3870
Pete Cooper75403d72015-06-24 20:22:23 +00003871CallInst *CallInst::cloneImpl() const {
Sanjoy Dasbd1c1bf2015-11-10 20:13:21 +00003872 if (hasOperandBundles()) {
3873 unsigned DescriptorBytes = getNumOperandBundles() * sizeof(BundleOpInfo);
3874 return new(getNumOperands(), DescriptorBytes) CallInst(*this);
3875 }
Devang Patel11cf3f42009-10-27 22:16:29 +00003876 return new(getNumOperands()) CallInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003877}
3878
Pete Cooper75403d72015-06-24 20:22:23 +00003879SelectInst *SelectInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003880 return SelectInst::Create(getOperand(0), getOperand(1), getOperand(2));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003881}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003882
Pete Cooper75403d72015-06-24 20:22:23 +00003883VAArgInst *VAArgInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003884 return new VAArgInst(getOperand(0), getType());
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003885}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003886
Pete Cooper75403d72015-06-24 20:22:23 +00003887ExtractElementInst *ExtractElementInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003888 return ExtractElementInst::Create(getOperand(0), getOperand(1));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003889}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003890
Pete Cooper75403d72015-06-24 20:22:23 +00003891InsertElementInst *InsertElementInst::cloneImpl() const {
Chris Lattner1dcb6542012-01-25 23:49:49 +00003892 return InsertElementInst::Create(getOperand(0), getOperand(1), getOperand(2));
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003893}
3894
Pete Cooper75403d72015-06-24 20:22:23 +00003895ShuffleVectorInst *ShuffleVectorInst::cloneImpl() const {
Chris Lattner1dcb6542012-01-25 23:49:49 +00003896 return new ShuffleVectorInst(getOperand(0), getOperand(1), getOperand(2));
Gabor Greif697e94c2008-05-15 10:04:30 +00003897}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003898
Pete Cooper75403d72015-06-24 20:22:23 +00003899PHINode *PHINode::cloneImpl() const { return new PHINode(*this); }
Devang Patel11cf3f42009-10-27 22:16:29 +00003900
Pete Cooper75403d72015-06-24 20:22:23 +00003901LandingPadInst *LandingPadInst::cloneImpl() const {
Bill Wendlingfae14752011-08-12 20:24:12 +00003902 return new LandingPadInst(*this);
3903}
3904
Pete Cooper75403d72015-06-24 20:22:23 +00003905ReturnInst *ReturnInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003906 return new(getNumOperands()) ReturnInst(*this);
3907}
3908
Pete Cooper75403d72015-06-24 20:22:23 +00003909BranchInst *BranchInst::cloneImpl() const {
Jay Foadd81f3c92011-01-07 20:29:02 +00003910 return new(getNumOperands()) BranchInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003911}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003912
Pete Cooper75403d72015-06-24 20:22:23 +00003913SwitchInst *SwitchInst::cloneImpl() const { return new SwitchInst(*this); }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003914
Pete Cooper75403d72015-06-24 20:22:23 +00003915IndirectBrInst *IndirectBrInst::cloneImpl() const {
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003916 return new IndirectBrInst(*this);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003917}
3918
Pete Cooper75403d72015-06-24 20:22:23 +00003919InvokeInst *InvokeInst::cloneImpl() const {
Sanjoy Dasbd1c1bf2015-11-10 20:13:21 +00003920 if (hasOperandBundles()) {
3921 unsigned DescriptorBytes = getNumOperandBundles() * sizeof(BundleOpInfo);
3922 return new(getNumOperands(), DescriptorBytes) InvokeInst(*this);
3923 }
Devang Patel11cf3f42009-10-27 22:16:29 +00003924 return new(getNumOperands()) InvokeInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003925}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003926
Pete Cooper75403d72015-06-24 20:22:23 +00003927ResumeInst *ResumeInst::cloneImpl() const { return new (1) ResumeInst(*this); }
Bill Wendlingf891bf82011-07-31 06:30:59 +00003928
David Majnemer654e1302015-07-31 17:58:14 +00003929CleanupReturnInst *CleanupReturnInst::cloneImpl() const {
3930 return new (getNumOperands()) CleanupReturnInst(*this);
3931}
3932
David Majnemer654e1302015-07-31 17:58:14 +00003933CatchReturnInst *CatchReturnInst::cloneImpl() const {
David Majnemer0bc0eef2015-08-15 02:46:08 +00003934 return new (getNumOperands()) CatchReturnInst(*this);
David Majnemer654e1302015-07-31 17:58:14 +00003935}
3936
David Majnemer8a1c45d2015-12-12 05:38:55 +00003937CatchSwitchInst *CatchSwitchInst::cloneImpl() const {
3938 return new CatchSwitchInst(*this);
3939}
3940
3941FuncletPadInst *FuncletPadInst::cloneImpl() const {
3942 return new (getNumOperands()) FuncletPadInst(*this);
David Majnemer654e1302015-07-31 17:58:14 +00003943}
3944
Pete Cooper75403d72015-06-24 20:22:23 +00003945UnreachableInst *UnreachableInst::cloneImpl() const {
Nick Lewycky42fb7452009-09-27 07:38:41 +00003946 LLVMContext &Context = getContext();
Devang Patel11cf3f42009-10-27 22:16:29 +00003947 return new UnreachableInst(Context);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003948}